R:计算边距或行&数据框的列总和
我有一个看起来像这样的数据框:
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
我需要以这样的方式求和,使我的数据框最终看起来像这样
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 14
7 B FIRST 0 0 0 0
8 B SECOND 3 9 0 12
9 B THIRD 5 85 0 90
10 B FOURTH 4 96 0 100
11 B FIFTH 3 40 0 43
12 B SIXTH 0 17 0 17
13 (all) FIRST 2 0 0 2
14 (all) SECOND 4 18 0 22
15 (all) THIRD 8 92 0 100
16 (all) FOURTH 13 114 0 127
17 (all) FIFTH 4 62 0 66
18 (all) SIXTH 1 30 0 31
19 A (all) 17 68 0 86
20 B (all) 15 247 0 262
21 (all) (all) 32 315 0 348
我已经尝试了 reshape2 包中的 add_margins 函数,没有用,它不会像我想要的那样计算总和它到。我尝试过aggregate、rowSums & colSums - 没有结果。
这里的任何帮助都会很棒。
谢谢
求和函数也需要添加之前的 Flag2 的总和。喜欢,
Flag1 Flag2 Type1 Type2 Type3 Sum
1 A FIRST 2 0 0 2
2 A SECOND 1 9 0 12
3 A THIRD 3 7 0 22
4 A FOURTH 9 18 0 49
5 A FIFTH 1 22 0 72
6 A SIXTH 1 13 0 86
7 B FIRST 0 0 0 0
8 B SECOND 3 9 0 12
9 B THIRD 5 85 0 102
10 B FOURTH 4 96 0 202
11 B FIFTH 3 40 0 245
12 B SIXTH 0 17 0 262
13 (all) FIRST 2 0 0 2
14 (all) SECOND 4 18 0 24
15 (all) THIRD 8 92 0 124
16 (all) FOURTH 13 114 0 251
17 (all) FIFTH 4 62 0 317
18 (all) SIXTH 1 30 0 348
19 A (all) 17 68 0 85
20 B (all) 15 247 0 262
21 (all) (all) 32 315 0 347
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 14
7 B FIRST 0 0 0 0
8 B SECOND 3 9 0 12
9 B THIRD 5 85 0 90
10 B FOURTH 4 96 0 100
11 B FIFTH 3 40 0 43
12 B SIXTH 0 17 0 17
13 (all) FIRST 2 0 0 2
14 (all) SECOND 4 18 0 22
15 (all) THIRD 8 92 0 100
16 (all) FOURTH 13 114 0 127
17 (all) FIFTH 4 62 0 66
18 (all) SIXTH 1 30 0 31
19 A (all) 17 68 0 86
20 B (all) 15 247 0 262
21 (all) (all) 32 315 0 348
I have tried the add_margins function in the reshape2 package, no use, it doesn't calculate the sums like I want it to. I have tried aggregate, rowSums & colSums - no result.
Any help here would be great.
Thanks
The summing function needs to add the previous Flag2's sum too. Like,
Flag1 Flag2 Type1 Type2 Type3 Sum
1 A FIRST 2 0 0 2
2 A SECOND 1 9 0 12
3 A THIRD 3 7 0 22
4 A FOURTH 9 18 0 49
5 A FIFTH 1 22 0 72
6 A SIXTH 1 13 0 86
7 B FIRST 0 0 0 0
8 B SECOND 3 9 0 12
9 B THIRD 5 85 0 102
10 B FOURTH 4 96 0 202
11 B FIFTH 3 40 0 245
12 B SIXTH 0 17 0 262
13 (all) FIRST 2 0 0 2
14 (all) SECOND 4 18 0 24
15 (all) THIRD 8 92 0 124
16 (all) FOURTH 13 114 0 251
17 (all) FIFTH 4 62 0 317
18 (all) SIXTH 1 30 0 348
19 A (all) 17 68 0 85
20 B (all) 15 247 0 262
21 (all) (all) 32 315 0 347
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有这样一个数据帧,其名称是 dtable:
我无法获得您想要的确切排序顺序,但这很接近:
Assume you have such a data,frame and its name is dtable:
I wasn't able to get the exact sort order that you wanted but this is close:
rowSums
对我有用(或者我错过了什么?)。rowSums
works for me (or am I missing something?).