适合ddply功能的替代方案

时间:2017-08-31 14:59:38

标签: r dataframe dplyr plyr

如何使用dplyr将以下tibble转换为下面发布的最终结果?

> group_by(hth, team) %>% arrange(team)
Source: local data frame [26 x 14]
Groups: team [13]

    team   CSK    DC    DD    GL   KKR   KTK  KXIP    MI    PW   RCB  RPSG
   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1    CSK     0     8    11     0    11     2     9    10     4    10     0
2    CSK     0     2     5     0     5     0     8    12     2     9     0
3     DC     2     0     8     0     2     1     7     5     3     8     0
4     DC     8     0     3     0     7     0     3     5     1     3     0
5     DD     5     3     0     0     7     2     8     5     2    10     2
6     DD    11     8     0     2    10     0    10    13     4     7     0
7     GL     0     0     2     0     0     0     0     0     0     1     0
8     GL     0     0     0     0     2     0     2     2     0     2     2
9    KKR     5     7    10     2     0     0     5    10     3    15     0
10   KKR    11     2     7     0     0     2    14     8     2     3     2
# ... with 16 more rows, and 2 more variables: RR <dbl>, SH <dbl>
> 

我使用了plyr的ddply函数,并且能够实现结果。

> ddply(hth, .(team), function(x) colSums(x[,-1], na.rm = TRUE))
   team CSK DC DD GL KKR KTK KXIP MI PW RCB RPSG RR SH
1   CSK   0 10 16  0  16   2   17 22  6  19    0 17  6
2    DC  10  0 11  0   9   1   10 10  4  11    0  9  0
3    DD  16 11  0  2  17   2   18 18  6  17    2 16  8
4    GL   0  0  2  0   2   0    2  2  0   3    2  0  3
5   KKR  16  9 17  2   0   2   19 18  5  18    2 15  9
6   KTK   2  1  2  0   2   0    1  1  1   2    0  2  0
7  KXIP  17 10 18  2  19   1    0 18  6  18    2 15  8
8    MI  22 10 18  2  18   1   18  0  6  19    2 16  8
9    PW   6  4  6  0   5   1    6  6  0   5    0  5  2
10  RCB  19 11 17  3  18   2   18 19  5   0    2 16  9
11 RPSG   0  0  2  2   2   0    2  2  0   2    0  0  2
12   RR  17  9 16  0  15   2   15 16  5  16    0  0  7
13   SH   6  0  8  3   9   0    8  8  2   9    2  7  0
> 

如何使用dplyr函数实现相同的目标?

1 个答案:

答案 0 :(得分:0)

您似乎按team进行分组并对dplyr中的列进行求和:

library(dplyr)

hth %>%
  group_by(team) %>%
  summarise_all(funs(sum), na.rm = TRUE)