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.
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()
:output