Like this: (Ideally in Java, Scala, C# but any well established computer language will do as well) so not pseudo code:
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
B W B W B W B W
W B W B W B W B
Techincally, I assume, it is quite easy (pseudo code):
function PrintFields(int index) {
int x=index % 8;
int y=index / 8;
int bw=index % 2;
print_at(x,y,(bw==0)?'B':'W');
if (index<63) PrintFields(index+1);
}
The question is, WHY ON EARTH would someone do that?
EDIT
I forgot the end condition, which is now in place
Related
This R code:
Iris <- data.frame(rbind(iris3[,,1], iris3[,,2], iris3[,,3]),
Sp = rep(c("s","c","v"), rep(50,3)))
train <- sample(1:150, 75)
z <- MASS::lda(Sp ~ ., Iris, prior = c(1,1,1)/3, subset = train)
MASS::predict.lda(z)
gives the following error message:
Error: 'predict.lda' is not an exported object from 'namespace:MASS'
The predict.lda function of MASS is documented but, apparently, not part of the package's namespace. Why not?
This problem is important because I need to use predict.lda in a package of my own and this error is making it fail the CRAN checks.
We can load the package and then use predict
library(MASS)
predict(z)
Or specify the :::. According to ?":::"
Accessing exported and internal variables, i.e. R objects (including lazy loaded data sets) in a namespace.
MASS:::predict.lda(z)
#$class
# [1] v s s s s c s v s v v v v c v v c v c s s s s c c v c v v c s s v c s s c v s c v v s c s c s c c s v c s s c s s c c c s c s v
#[65] v v v s c s c v v s s
#Levels: c s v
#$posterior
# c s v
#107 3.513603e-03 1.352029e-37 9.964864e-01
#37 2.749629e-26 1.000000e+00 5.088976e-50
# ...
Or another option is to get the function from name space
predictlda <- getFromNamespace("predict.lda", "MASS")
predictlda(z)
#$class
# [1] v s s s s c s v s v v v v c v v c v c s s s s c c v c v v c s s v c s s c v s c v v s c s c s c c s v c s s c s s c c c s c s v
#[65] v v v s c s c v v s s
#Levels: c s v
#$posterior
# c s v
#107 3.513603e-03 1.352029e-37 9.964864e-01
#37 2.749629e-26 1.000000e+00 5.088976e-50
# ..
Well, I'm trying to do a maze-game, Pac-man, I need creation and render of map, for this, I did a "map of vectors" with the different states that I need.
This is the code:
#lang racket
(require 2htdp/image 2htdp/universe
(only-in racket/draw
read-bitmap))
(require math/matrix)
(require math/array)
(define CELL-SIZE 20)
(define E (rectangle CELL-SIZE CELL-SIZE "solid" "black"))
(define D (circle 3 "solid" "YELLOW"))
(define W (rectangle CELL-SIZE CELL-SIZE "solid" "blue"))
(define C (rectangle CELL-SIZE CELL-SIZE "solid" "red"))
(define Initial-Map
(vector (vector W W W W W W W W W W W W W)
(vector W C D D D D D D D D D D W)
(vector W D W D W W W W W D W D W)
(vector W D W D W D D D W D W D W)
(vector W D W D D D W D D D W D W)
(vector W D W W D W W W D W W D W)
(vector E D D D D D E D D D D D E)
(vector W D W W D W W W D W W D W)
(vector W D W D D D W D D D W D W)
(vector W D W D W D W D W D W D W)
(vector W D W D D D W D D D W D W)
(vector W D W D W D D D W D W D W)
(vector W D D C W D W D W D D D W)
(vector W W W W W W W W W W W W W)))
(define EMPTY (rectangle CELL-SIZE CELL-SIZE "solid" "black")) ; Empty
(define DOT (circle 3 "solid" "white")) ; DOT
(define WALL (rectangle CELL-SIZE CELL-SIZE "solid" "blue")) ; Wall
(define Scene (empty-scene 400 400 "black"))
(define (paintWorld mundo) (place-image (vector*->matrix Initial-Map) 100 100 Scene))
(big-bang 4
;(on-key changePacmanM)
;(on-tick automaticMovePacman 0.2)
(to-draw paintWorld)
(state true)
(name "Pacman - Racket"))
This is the problem
Initial-Map is the map vector.
How can i render it for the big-bang creation universe function?
I tried to convert vector to matrix with vector*->matrix in order to pass it to place-image:
(place-image (vector*->matrix Initial-Map) 100 100 Scene)
But I get the following error:
place-image: expects an image as first argument, given (mutable-array #[#[(object:image% ...) (object:image% ...) (object:image% ...) (object:image% ...) (object:image% ...)
I a dataframe which I have subsetted using normal indexing. Code below.
dframe <- dframe[1:10, c(-3,-7:-10)]
But when I write dframe$Symbol I get the output.
BABA ORCL LFC TSM ACT ABBV MA ABEV KMI UPS
3285 Levels: A AA AA^B AAC AAN AAP AAT AAV AB ABB ABBV ABC ABEV ABG ABM ABR ABR^A ABR^B ABR^C ABRN ABT ABX ACC ACCO ACE ACG ACH ACI ACM ACN ACP ACRE ACT ACT^A ACW ADC ADM ADPT ADS ADT ADX AEB AEC AED AEE AEG AEH AEK AEL AEM AEO AEP AER AES AES^C AET AF AF^C ... ZX
I'm wondering what is happening here. Does the dframe dataframe only contain 10 rows or still all rows, but only outputs 10 rows?
Thanks
That's just the way factors work. When you subset a factor, it preserves all levels, even those that are no longer represented in the subset. For example:
f1 <- factor(letters);
f1;
## [1] a b c d e f g h i j k l m n o p q r s t u v w x y z
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
f2 <- f1[1:10];
f2;
## [1] a b c d e f g h i j
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
To answer your question, it's actually slightly tricky to append all missing levels to a factor. You have to combine the existing factor data with all missing indexes (here I'm referring to the integer indexes that the factor class internally uses to map the actual factor data to its levels vector, which is stored as an attribute on the factor object), and then rebuild a factor (using the original levels) from that combined data. Below I demonstrate this, now randomizing the subset taken from f1 to demonstrate that order does not matter:
set.seed(1); f3 <- sample(f1,10);
f3;
## [1] g j n u e s w m l b
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
factor(c(f3,setdiff(1:nlevels(f3),as.integer(f3))),labels=levels(f3));
## [1] g j n u e s w m l b a c d f h i k o p q r t v x y z
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
I have a data frame with several columns of varied character data. I want to find the average of each combination of that character data. I think I'm closing in on a solution, but am having trouble figuring out how to loop over characters. An example bit of data would be like:
Var1 Var2 Var3 M1
a w j 20
a w j 15
a w k 10
a w j 0
b x L 30
b x L 10
b y k 20
b y k 15
c z j 20
c z j 10
c z k 11
c w l 45
a d j 20
a d k 4
a d l 23
a d k 11
And trying to get it in the form of:
P1 P2 P3 Avg
a w j 11.667
a w k 10
a d j 20
a d k 15
a d l 23
b x L 20
b y k 17.5
c z j 15
c z k 11
c w l 45
I think the idea is something like:
test <- read.table("clipboard",header=T)
newdata <- subset(test,
Var1=='a'
& Var2=='w'
& Var3=='j',
select=M1
)
row.names(newdata)<-NULL
newdata2 <- as.data.frame(matrix(data=NA,nrow=3,ncol=4))
names(newdata2) <- c("P1","P2","P3","Avg")
newdata2[1,1] <- 'a'
newdata2[1,2] <- 'w'
newdata2[1,3] <- 'j'
newdata2[1,4] <- mean(newdata$M1)
Which works for the first line, but I'm not entirely sure how to automate this to loop over each character combination across the columns. Unless, of course, there's a similar apply-like function to use in this case?
library(dplyr)
newdata2 = summarise(group_by(test,Var1,Var2,Var3),Avg=mean(M1))
And the result:
> newdata2
Source: local data frame [10 x 4]
Groups: Var1, Var2
Var1 Var2 Var3 Avg
1 a d j 20.00000
2 a d k 7.50000
3 a d l 23.00000
4 a w j 11.66667
5 a w k 10.00000
6 b x L 20.00000
7 b y k 17.50000
8 c w l 45.00000
9 c z j 15.00000
10 c z k 11.00000
Using the base aggregate function:
mydata <- read.table(header=TRUE, text="
Var1 Var2 Var3 M1
a w j 20
a w j 15
a w k 10
a w j 0
b x L 30
b x L 10
b y k 20
b y k 15
c z j 20
c z j 10
c z k 11
c w l 45
a d j 20
a d k 4
a d l 23
a d k 11")
aggdata <-aggregate(mydata$M1, by=list(mydata$Var1,mydata$Var2,mydata$Var3) , FUN=mean, na.rm=TRUE)
output:
> aggdata
Group.1 Group.2 Group.3 x
1 a d j 20.00000
2 a w j 11.66667
3 c z j 15.00000
4 a d k 7.50000
5 a w k 10.00000
6 b y k 17.50000
7 c z k 11.00000
8 a d l 23.00000
9 c w l 45.00000
10 b x L 20.00000
I need to store a very long list of variable names in Stata and after something like 250 characters, no more characters can be stored in a local or global macro. Currently, I'm using many different globals to store the names of the many regressors that I am using but I would much prefer to keep them all in one.
EDIT: The question has been perfectly answered by Maarten below but I would just add the code I was using for precision.
local RHSVARS = "var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var12 var13 var14 var15 var16 var17 var18 var19"
does not work but
local RHSVARS "var1 var2 var3 var4 var5 var6 var7 var8 var9 var10 var11 var12 var13 var14 var15 var16 var17 var18 var19"
does.
This issue is largely solved in Stata 13, so I guess you have an older version.
You can still do so in older versions by just leaving out the equal sign, which you can see in the example below (it ran in Stata 12, in Stata 13 both macros are not truncated). This is discussed in the following article: Nicholas J. Cox (2008) "Stata tip 70: Beware the evaluating equal sign" The Stata Journal, 8(4): 586-587. It is now freely available here: http://www.stata-journal.com/article.html?article=pr0045
. // create local a with an equal sign
. local a = "`c(alpha)' `c(ALPHA)' `c(alpha)' `c(ALPHA)' `c(alpha)' `c(ALPHA)'"
. // create local b by leaving the equal sign out
. local b "`c(alpha)' `c(ALPHA)' `c(alpha)' `c(ALPHA)' `c(alpha)' `c(ALPHA)'"
.
. // local macro a gets truncated
. di `: length local a'
245
. di "`a'"
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X
> Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T
> U V W X Y Z a b c d e f g h i j k l m n o p q r s
.
. // local macro b does not get truncated
. di `: length local b'
311
. di "`b'"
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X
> Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T
> U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q
> R S T U V W X Y Z