我在R中有一个数据框,我想重新组织。请考虑以下事项:
refreshToken()
给出以下内容:
samples=c("167_1", "167_2", "167_3", "167_4", "167_5", "167_6", "167_7", "167_8", "167_9", "167_10", "167_11", "167_12", "167_13", "167_14", "167_15")
condition=c("Group4", "Group7", "Group8", "Group3", "Group4", "Group2", "Group6", "Group1", "Group2", "Group9", "Group7", "Group8", "Group3", "Group5", "Group5")
df=data.frame(samples, condition)
我想重新整理数据:
> head(df)
samples condition
1 167_1 Group4
2 167_2 Group7
3 167_3 Group8
4 167_4 Group3
5 167_5 Group4
6 167_6 Group2
我尝试过使用reshape2,我可以从长格式到宽格式但是我不确定如何从NAs的混乱进展到汇总列表。
condition samples
Group1 167_8
Group2 167_6, 167_9
Group3 167_13, 167_4
Group4 167_1, 167_5
Group5 167_14, 167_15
Group6 167_7
Group7 167_11, 167_2
Group8 167_12, 167_3
Group9 167_10
非常感谢任何帮助,谢谢。
答案 0 :(得分:2)
您可以使用dplyr
执行此操作,如下所示:
library(dplyr)
df %>%
group_by(condition) %>%
summarise(samples = paste(samples, collapse = ", "))
结果:
# A tibble: 9 × 2
condition samples
<fctr> <chr>
1 Group1 167_8
2 Group2 167_6, 167_9
3 Group3 167_4, 167_13
4 Group4 167_1, 167_5
5 Group5 167_14, 167_15
6 Group6 167_7
7 Group7 167_2, 167_11
8 Group8 167_3, 167_12
9 Group9 167_10