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 > > >Any idea how to get 20 crosstables within a loop? > >Thank you for any hints, >Stefan ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.