我想同时为分组的数据帧应用dplyr::summarisedplyr::summarise_each。可能吗?

我的数据如下所示:



mydf <- data.frame(
    id = c(rep(1,2), rep(2, 3), rep(3, 4)),
    amount = c(rep(1,4), rep(2,5)),
    type1 = c(rep(1, 2), rep(0, 7)),
    type2 = c(rep(0, 4), rep(1, 5))
)
mydf
#  id amount type1 type2
#1  1      1     1     0
#2  1      1     1     0
#3  2      1     0     0
#4  2      1     0     0
#5  2      2     0     1
#6  3      2     0     1
#7  3      2     0     1
#8  3      2     0     1
#9  3      2     0     1


我想对id变量amount求和,并获得type变量的最大值。我知道我可以这样做,如下所示:

mydf %>%
    group_by(id) %>%
    summarise(amount = sum(amount), type1 = max(type1), type2 = max(type2))


但是,我有很多type变量,所以我更喜欢这样的东西(但也要加上amount)。

mydf %>%
    group_by(id) %>%
    summarise_each(funs(max), matches("type"))

最佳答案

使用dplyr

library(dplyr)

mydf %>%
     group_by(id) %>%
     mutate(amount = sum(amount)) %>%
     mutate_each(funs(max), matches("type")) %>%
     unique

#Source: local data table [3 x 4]

#  id amount type1 type2
#1  1      2     1     0
#2  2      4     0     1
#3  3      8     0     1


或如@HongOoi所示

mydf %>%
     group_by(id) %>%
     mutate(amount=sum(amount)) %>%
     summarise_each(funs(max))

08-19 20:06