Assuming that the CrossTable() function is contained in the descr package, it seems that the argument to dnn gives the row and column names in the crosstabulation. The trick is to get lapply to read both the names and the data. names(mydata)[2:4] gives the names; mydata[, 2:4] is the data. The syntax for lapply is:
lapply(x, FUN, ...)
FUN is applied to each element of x, and ... allows optional arguments to be passed to FUN. Thus, both names(mydata)[2:4] and mydata[, 2:4] can be passed FUN.
mydata<-data.frame(matrix(rep(c(1:2),times= 50),20,5))
colnames(mydata)<-letters[1:5]
library(descr)
lapply(names(mydata)[2:4],
function(dfNames, dfData) {
return(CrossTable(dfData[[dfNames]], mydata[,5], dnn = c(dfNames, "mydata[,5]")))
}, mydata[, 2:4] )
The function operates on each element in names(mydata)[2:4], and the data file is passed as an additional parameter. This way, the relevant column (dfData[[dfNames]]) and the name of the relevant column (dfName) are available to CrossTable.
[[1]]
Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
===============================
mydata[,5]
b 1 2 Total
-------------------------------
1 10 0 10
5.000 5.000
1.000 0.000 0.500
1.000 0.000
0.500 0.000
-------------------------------
2 0 10 10
5.000 5.000
0.000 1.000 0.500
0.000 1.000
0.000 0.500
-------------------------------
Total 10 10 20
0.500 0.500
===============================
[[2]]
Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
===============================
mydata[,5]
c 1 2 Total
-------------------------------
1 10 0 10
5.000 5.000
1.000 0.000 0.500
1.000 0.000
0.500 0.000
-------------------------------
2 0 10 10
5.000 5.000
0.000 1.000 0.500
0.000 1.000
0.000 0.500
-------------------------------
Total 10 10 20
0.500 0.500
===============================
[[3]]
Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
===============================
mydata[,5]
d 1 2 Total
-------------------------------
1 10 0 10
5.000 5.000
1.000 0.000 0.500
1.000 0.000
0.500 0.000
-------------------------------
2 0 10 10
5.000 5.000
0.000 1.000 0.500
0.000 1.000
0.000 0.500
-------------------------------
Total 10 10 20
0.500 0.500
===============================