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
Post a Comment