R: calculating margins or row & col sums for a data frame
问题 I have a data frame that looks like this: Flag1 Flag2 Type1 Type2 Type3 1 A FIRST 2 0 0 2 A SECOND 1 9 0 3 A THIRD 3 7 0 4 A FOURTH 9 18 0 5 A FIFTH 1 22 0 6 A SIXTH 1 13 0 7 B FIRST 0 0 0 8 B SECOND 3 9 0 9 B THIRD 5 85 0 10 B FOURTH 4 96 0 11 B FIFTH 3 40 0 12 B SIXTH 0 17 0 I need to sum in such a way that my data frame finally looks like this Flag1 Flag2 Type1 Type2 Type3 Sum 1 A FIRST 2 0 0 2 2 A SECOND 1 9 0 10 3 A THIRD 3 7 0 10 4 A FOURTH 9 18 0 27 5 A FIFTH 1 22 0 23 6 A SIXTH 1 13 0