How create the frequency table with multiple columns in R?

2.9k views Asked by At

I am trying to make a frequency table with multiple columns. My data is

C = as.factor(sample( LETTERS[1:2], 100, replace = TRUE, prob = c(rep(1/2, 2))))
R1 = sample(c(-1, 1), 100, replace = TRUE)
R2 = sample(c(-1, 1), 100, replace = TRUE)
R3 = sample(c(-1, 1), 100, replace = TRUE)
data = data.frame(R1, R2, R3, C)
rowb = expand.grid(data.frame(r1 = c(-1, 1), r2 = c(-1, 1), r3 = c(-1, 1)))

My goal frequency table is that row contains a combination of rowb, and column is C. At this time, R1, R2,and R3 are matched with the row. If no match is found, the value of the element is zero.

I attached a structure for goal frequency table.

enter image description here

2

There are 2 answers

0
scoa On

Based on the image you provided, it seems you just want to create a new variable concatenating R1-3 and then tabulate it with C; you could just use paste():

data$comb <- factor(paste(data$R1, data$R2, data$R3),
# make sure the levels are in the right order:
                    levels = paste(rowb$r1, rowb$r2, rowb$r3))

table(data$comb, data$C)

output

            A  B
  -1 -1 -1  5  5
  1 -1 -1   6  4
  -1 1 -1   6  5
  1 1 -1    8 10
  -1 -1 1   7  4
  1 -1 1    9  5
  -1 1 1    5  7
  1 1 1     4 10
0
akrun On

We can use dcast from data.table and join with 'rowb'

library(data.table)
dcast(setDT(data), R1+R2+R3~C, length)[rowb, on = .(R1=r1, R2 = r2, R3 = r3)]

Or do the join first and then dcast

dcast(setDT(data)[rowb , on = .(R1=r1, R2 = r2, R3 = r3)], ...~C)