r - Sum in a Cross Table -


i new in r. apologies if questions silly.

i have sample input data table follows.

column-1 column-2 column-3   ff       ff       2  nn       nn       5  ff       ff       2  nn       nn       1  fn       ff       3 

output should

       ff   fn  nf  nn     ff  4   0   0   0     fn  3   0   0   0     nf  0   0   0   0     nn  0   0   0   6 

i have used table() function, gives "count", , not "sum". appreciate help!

with little known xtabs function. data:

l <- c("ff", "fn", "nf", "nn")  data <- data.frame(    column1 = factor(c("ff", "nn", "ff", "nn", "fn"), levels = l),    column2 = factor(c("ff", "nn", "ff", "nn", "ff"), levels = l),    column3 = c(2, 5, 2, 1, 3)) 

xtabs(column3 ~ ., data) #        column2 # column1 ff fn nf nn #      ff  4  0  0  0 #      fn  3  0  0  0 #      nf  0  0  0  0 #      nn  0  0  0  6 

the output table can wrap as.matrix matrix.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -