I have a data frame that looks like this:
structure(list(ab = c(0, 1, 1, 1, 1, 0, 0, 0, 1, 1), bc = c(1, 1, 1, 1, 0, 0, 0, 1, 0, 1), de = c(0, 0, 1, 1, 1, 0, 1, 1, 0, 1), cl = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 2)), .Names = c("ab", "bc", "de", "cl"), row.names = c(NA, -10L), class = "data.frame")
The cl column indicates cluster association and the variables ab, bc and carry binary answers, where 1 indicates yes and 0 indicates no.
I am trying to create a cluster with crosstab tables along with all the other columns in the data frame: ab, bc and de, where the clusters become columns of variables. The desired result looks like this:
1 2 3 ab 1 3 2 bc 2 3 1 de 2 3 1
I tried the following code:
with(newdf, tapply(newdf[,c(3)], cl, sum))
This gives me cross tabbing values ββonly one column at a time. In my data frame there are 1600+ columns with 1 column of the cluster. Can anyone help?