I have this kind of table:
aaa bbb ccc
A 0 3 5
B 2 2 2
C 2 5 7
I get it using anti_join
(for a table with non numeric values) and table
command to group the result in a nicer way (something like countif in Excel).
da1 <- anti_join(data1,data2, by=c("pam1","pam2"))
table(da1$pam1,da1$pam2)
I was wondering if is possible to add also a Sum of each row and each column, so that in the result there will be something like this:
aaa bbb ccc SUM
A 0 3 5 8
B 2 2 2 6
C 2 5 7 14
SUM 4 10 14
We can try with rowSums
and colSums
cbind(rbind(df, SUM = rowSums(df)), SUM = c(colSums(df), NA))
# aaa bbb ccc SUM
#A 0 3 5 4
#B 2 2 2 10
#C 2 5 7 14
#SUM 8 6 14 NA
An elegant option is addmargins
addmargins(as.matrix(df1))
# aaa bbb ccc Sum
#A 0 3 5 8
#B 2 2 2 6
#C 2 5 7 14
#Sum 4 10 14 28
df1 <- structure(list(aaa = c(0L, 2L, 2L), bbb = c(3L, 2L, 5L), ccc = c(5L,
2L, 7L)), .Names = c("aaa", "bbb", "ccc"), class = "data.frame", row.names = c("A",
"B", "C"))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.