我有这样data.frame的形式,如:
my_df <- data.frame(id = c(1, 1, 2, 3),
title = c("YourMa", "YourMa", "MyMa", "HisMa"),
autqty = c(2, 2, 1, 1),
aut = c("Steve", "Joe", "Albert", "Kevin"),
pubb = c("Good", "Good", "Meh", "Fan"))
Run Code Online (Sandbox Code Playgroud)
看起来像:
> my_df
id title autqty aut pubb
1 YourMa 2 Steve Good
1 YourMa 2 Joe Good
2 MyMa 1 Albert Meh
3 HisMa 1 Kevin Fan
Run Code Online (Sandbox Code Playgroud)
请注意,id 1除了一个aut条目外,所有信息都相同. 我的目标是减少数据my_df,将aut数据合并为一个元素:
id title autqty aut pubb
1 YourMa 2 Steve, Joe Good
2 MyMa 1 Albert Meh
3 HisMa 1 Kevin Fan
Run Code Online (Sandbox Code Playgroud)
注意:这是我原始数据的较小版本.我希望能够处理任何数量的aut事件.
使用group_by和summarise在dplyr:
my_df %>%
group_by(id, title, autqty, pubb) %>%
summarise(aut=paste(aut, collapse=", ")) %>%
ungroup()
# A tibble: 3 × 5
id title autqty pubb aut
<dbl> <fctr> <dbl> <fctr> <chr>
1 1 YourMa 2 Good Steve, Joe
2 2 MyMa 1 Meh Albert
3 3 HisMa 1 Fan Kevin
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71 次 |
| 最近记录: |