R:计算边距或行&数据框的列总和

发布于 2024-11-04 11:17:27 字数 3646 浏览 4 评论 0原文

我有一个看起来像这样的数据框:

         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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2024-11-11 11:17:28

假设您有这样一个数据帧,其名称是 dtable:

dt1 <- as.data.frame(addmargins(xtabs(Type1~Flag1+Flag2, data=dtable)))
dt2 <- as.data.frame(addmargins(xtabs(Type2~Flag1+Flag2, data=dtable)))
dt3 <- as.data.frame(addmargins(xtabs(Type3~Flag1+Flag2, data=dtable)))
names(dt1)[3] <- "Type1"
names(dt2)[3] <- "Type2"
names(dt3)[3] <- "Type3"

dt.all <- merge(merge(dt1,dt2), dt3)
dt.all$Sum <- with(dt.all, Type1+Type2+Type3)

我无法获得您想要的确切排序顺序,但这很接近:

levels(dt.all$Flag2) <-  c("FIRST", "SECOND", "THIRD", "FOURTH" ,"FIFTH", "SIXTH",  "Sum" ) 
dt.all[order(dt.all$Flag1, dt.all$Flag2), ]

   Flag1  Flag2 Type1 Type2 Type3 Sum
1      A  FIRST     1    22     0  23
2      A SECOND     2     0     0   2
3      A  THIRD     9    18     0  27
4      A FOURTH     1     9     0  10
5      A  FIFTH     1    13     0  14
7      A  SIXTH     3     7     0  10
6      A    Sum    17    69     0  86
8      B  FIRST     3    40     0  43
9      B SECOND     0     0     0   0
10     B  THIRD     4    96     0 100
11     B FOURTH     3     9     0  12
12     B  FIFTH     0    17     0  17
14     B  SIXTH     5    85     0  90
13     B    Sum    15   247     0 262
15   Sum  FIRST     4    62     0  66
16   Sum SECOND     2     0     0   2
17   Sum  THIRD    13   114     0 127
18   Sum FOURTH     4    18     0  22
19   Sum  FIFTH     1    30     0  31
21   Sum  SIXTH     8    92     0 100
20   Sum    Sum    32   316     0 348

Assume you have such a data,frame and its name is dtable:

dt1 <- as.data.frame(addmargins(xtabs(Type1~Flag1+Flag2, data=dtable)))
dt2 <- as.data.frame(addmargins(xtabs(Type2~Flag1+Flag2, data=dtable)))
dt3 <- as.data.frame(addmargins(xtabs(Type3~Flag1+Flag2, data=dtable)))
names(dt1)[3] <- "Type1"
names(dt2)[3] <- "Type2"
names(dt3)[3] <- "Type3"

dt.all <- merge(merge(dt1,dt2), dt3)
dt.all$Sum <- with(dt.all, Type1+Type2+Type3)

I wasn't able to get the exact sort order that you wanted but this is close:

levels(dt.all$Flag2) <-  c("FIRST", "SECOND", "THIRD", "FOURTH" ,"FIFTH", "SIXTH",  "Sum" ) 
dt.all[order(dt.all$Flag1, dt.all$Flag2), ]

   Flag1  Flag2 Type1 Type2 Type3 Sum
1      A  FIRST     1    22     0  23
2      A SECOND     2     0     0   2
3      A  THIRD     9    18     0  27
4      A FOURTH     1     9     0  10
5      A  FIFTH     1    13     0  14
7      A  SIXTH     3     7     0  10
6      A    Sum    17    69     0  86
8      B  FIRST     3    40     0  43
9      B SECOND     0     0     0   0
10     B  THIRD     4    96     0 100
11     B FOURTH     3     9     0  12
12     B  FIFTH     0    17     0  17
14     B  SIXTH     5    85     0  90
13     B    Sum    15   247     0 262
15   Sum  FIRST     4    62     0  66
16   Sum SECOND     2     0     0   2
17   Sum  THIRD    13   114     0 127
18   Sum FOURTH     4    18     0  22
19   Sum  FIFTH     1    30     0  31
21   Sum  SIXTH     8    92     0 100
20   Sum    Sum    32   316     0 348
你另情深 2024-11-11 11:17:28

rowSums 对我有用(或者我错过了什么?)。

> my.df <- read.table(textConnection("         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
+ "))
Browse[2]> my.df
   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
Browse[2]> rowSums(my.df[3:5])
  1   2   3   4   5   6   7   8   9  10  11  12 
  2  10  10  27  23  14   0  12  90 100  43  17 
Browse[2]> my.df$Sum <- rowSums(my.df[3:5])
Browse[2]> my.df
   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

rowSums works for me (or am I missing something?).

> my.df <- read.table(textConnection("         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
+ "))
Browse[2]> my.df
   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
Browse[2]> rowSums(my.df[3:5])
  1   2   3   4   5   6   7   8   9  10  11  12 
  2  10  10  27  23  14   0  12  90 100  43  17 
Browse[2]> my.df$Sum <- rowSums(my.df[3:5])
Browse[2]> my.df
   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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文