汇总基于多个列的数据框

时间:2019-10-01 14:29:29

标签: r aggregate summarize

我很难弄清楚这个。说,我有一张这样的桌子:

    Name Activity Day
1   John    cycle   1
2   John     work   1
3   Tina     work   1
4 Monika     work   1
5   Tina     swim   1
6   Tina  jogging   2
7   John     work   2
8   Tina     work   2

我想用一种方式来总结它,即每个人的活动都按照日期进行分组。

它应该像这样:

    Name      Activity Day
1   John    cycle;work   1
2   Tina     work;swim   1
3 Monika          work   1
4   Tina  jogging;work   2
5   John          work   2

我认为dplyr包将是这里的答案,但我不知道该怎么做。有帮助吗?

谢谢!

3 个答案:

答案 0 :(得分:0)

尝试:

library(dplyr)

dat <- tribble(~"Name", ~"Activity", ~"Day",
   "John",    "cycle",   1,
   "John",     "work" ,  1,
   "Tina",     "work",   1,
 "Monika",    "work",   1,
   "Tina",     "swim",   1,
   "Tina",  "jogging",   2,
   "John",     "work",   2,
   "Tina",     "work",  2)

dat %>% 
  group_by(Name, Day) %>% 
  summarise(activity = paste(Activity, collapse = "; "))

# A tibble: 5 x 3
# Groups:   Name [3]
  Name     Day activity     
  <chr>  <dbl> <chr>        
1 John       1 cycle; work  
2 John       2 work         
3 Monika     1 work         
4 Tina       1 work; swim   
5 Tina       2 jogging; work

答案 1 :(得分:0)

您可以使用aggregate函数,例如:

> aggregate(dat$Activity,list(dat$Name,dat$Day),as.character)
  Group.1 Group.2             x
1    John       1   cycle, work
2  Monika       1          work
3    Tina       1    work, swim
4    John       2          work
5    Tina       2 jogging, work

答案 2 :(得分:0)

带有data.table

的选项
library(data.table)
setDT(dat)[, .(Activity = toString(Activity)), .(Name, Day)]