R - 组合行和重命名值

时间:2017-09-28 13:13:34

标签: r dataframe merge dplyr

我正在尝试在重新编码以下数据框中的值时合并行:

     Days    Object   Frequency
1    1       Fruit    20
2    2       Fruit    21
3    3       Fruit    41
4    4       Fruit    12
5    5       Fruit    1   
6    6       Fruit    9
8    8       Fruit    1
9    9       Fruit    14

基本上,我想将这些日子分组为这样的分类变量:

    Days    Object    Frequency
1   1-2     Fruit     41
2   3-4     Fruit     43
3   5+      Fruit     25

在为“天”列创建新值时是否有任何合并方法?

如果这是一个愚蠢的问题,请道歉

2 个答案:

答案 0 :(得分:3)

您可以动态地在group_by中创建群组变量,然后进行汇总(假设您希望按Object分组):

df %>% 
    group_by(Days = if_else(Days %in% c(1,2), "1-2", if_else(Days %in% c(3,4), "3-4", "5+")), 
             Object) %>% 
    summarise(Frequency = sum(Frequency))

# A tibble: 3 x 3
# Groups:   Days [?]
#   Days Object Frequency
#  <chr> <fctr>     <int>
#1   1-2  Fruit        41
#2   3-4  Fruit        53
#3    5+  Fruit        25

答案 1 :(得分:3)

在基地R中,您可以合并cutaggregate。在这里,cut生成日期组,并为这些组提供标签。这将在包含对象aggregate的列表中提供,以执行完整分组。 aggregate将频率作为其第一个参数,并应用sum

aggregate(dat$Frequency, list(Days=cut(dat$Days, c(-Inf, 2, 4, Inf),
                                       labels=c("1-2", "2-4", "5+")),
                              object=dat$Object),
          sum)

返回

  Days object  x
1  1-2  Fruit 41
2  2-4  Fruit 53
3   5+  Fruit 25

要重命名x变量,您可以将其包装在setNames中,或者只在第二行中使用names<-

与此相当的data.table

library(data.table)
setDT(dat)[, sum(Frequency),
           by=list(Days=cut(dat$Days, c(-Inf, 2, 4, Inf), labels=c("1-2", "2-4", "5+")),
                   object=dat$Object)]
   Days object V1
1:  1-2  Fruit 41
2:  2-4  Fruit 53
3:   5+  Fruit 25