[R] Loop for CrossTable (gmodels)

David Winsemius dwinsemius at comcast.net
Sun May 12 19:44:43 CEST 2013


On May 12, 2013, at 10:30 AM, arun wrote:

> Hi,
> According to the error, the variables should have the same length.
> For example:
> set.seed(24)
> dat1<- cbind(RACE=sample(1:10,10,replace=TRUE),as.data.frame(matrix(sample(1:100,20*10,replace=TRUE),ncol=20)))
>  lapply(dat1[,-1],function(x) CrossTable(x,dat1$RACE,format="SPSS",prop.chisq=FALSE,digits=2,dnn=c("VAR","RACE"))) # prints cross tables.
> 
> #or
>  lapply(names(dat1)[-1],function(x) CrossTable(dat1[,x],dat1[,"RACE"],format="SPSS",prop.chisq=FALSE,digits=2,dnn=c(x,"RACE")))
> A.K.
>> Hi, 
>> 
>> I have 20 variables in a data frame (VAR1 ... VAR20) which I would like to crosstab against the variable RACE. 
>> I would like to use a loop structure instead of 20 statements like: 
>> CrossTable(VAR1, RACE, format = "SPSS", prop.chisq = FALSE, digits = 2) 
>> 
>> 
>> I have tried following syntax, but failed: 
>> 
>> library(gmodels) 
>> for(i in 1:20){ 
>> columnname <- ("VAR",i) 
>> CrossTable(columnname, RACE, format = "SPSS", prop.chisq = FALSE, digits = 2) 
>> } 
>> 
>> I receive following Error: 
>> Error in CrossTable(columnname, RACE, format = "SPSS", prop.chisq = FALSE,  : 
>  > x and y must have the same length 

It might be productive in learning R to understand what you were doing wrong and how you could have used that control for-loop structure. It does appear that you have `attach`-ed a data.frame and are referring to the column names. Yes? If so, you should realize that is not a particularly safe practice, but let's push on.

columnname is just  a character vector with a single element, "VAR1" the first time around. R does not do a double-evaluation to first figure out that `columnname` is "VAR1" and then proceed further to look up its value. To do that you would need to add `get`:

for(i in 1:20){ 
   columnname <- ("VAR",i) 
   CrossTable( get(columnname), RACE, format = "SPSS", prop.chisq = FALSE, digits = 2) 
} 

The get function does the extra step of converting the character value to an object name and returning the value of that named data-object.

>> Any idea how to get 20 crosstables within a loop? 

That should do it. It would have been better if you had used dput() to produce a workable small example of a few of the columns.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list