Reputation: 27
I am trying a loop to create the cross table. My data set is :21 columns, 1300 rows. I want to create the cross table for the column 1 with the column 1,the column 1 with column 2,the column 1 with column 3,...,the column 1 with column 21. I use the function "table" and try to loop. Her is my code:
x=data
for (i in length (x)){tab[i]=table(x[,1],x[i])
where tab is the output of the cross table.
I try to run,but it failed. Could you please let me know how to fix the problem? It give the 20 same warnings: In tab[i]=table(x[,1],x[,i]) number of items to replace is not a multiple of replacement length
Upvotes: 1
Views: 1633
Reputation: 887088
May be this helps
set.seed(25)
mat1 <- matrix( sample(1:10, 100,replace=TRUE), 10,10)
fun1 <- function(dat, colN) {
tab <- vector("list", colN)
for (i in seq_len(ncol(dat))) {
tab[[i]] <- table(dat[, 1], dat[, i])
}
tab
}
res <- fun1(mat1, 10) ##change the column number accordingly
Upvotes: 1