I've got a data frame in R that looks like this:
df1 <- data.frame(id = letters[seq( from = 1, to = 20 )],
var1 = sample(1:5,20,replace=T),
var2 = sample(1:5,20,replace=T))
Herevar1
and var2
are categorical variables between 1 and 5.
I want to create a matrix where the column headings are var1
1 to 5, the row headings are var2
1 to 5, and the matrix is populated by the count of the numbers of entries that belong to each group.
I've tried using reshape
, looked at the lazyeval
package, and found questions that are similar like this, but I can't get any to fit the bill.