I'm not quite sure what you used, but table works great for me!
Here's a minimal reproducible example:
df <- structure(list(V1 = c("a", "a", "a", "b", "b", "b", "c"), V2 = c("g", "h", "g", "i", "g", "h", "i")), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, -7L)) table(df) # V2 # V1 ghi # a 2 1 0 # b 1 1 1 # c 0 0 1
Notes:
- Try
table(df[c(2, 1)]) (or table(df$V2, df$V1) ) to exchange rows and columns. - Use
as.data.frame.matrix(table(df)) to get data.frame as output. ( as.data.frame will create a long data.frame , not the one you need in the same format).
source share