R:如何根据单个列中的唯一值合并来自多个列的重复行,并用|合并这些唯一值? [英] R: How to combine duplicated rows from multiple columns based on unique values in a single column and merge those unique values by |?

查看:97
本文介绍了R:如何根据单个列中的唯一值合并来自多个列的重复行,并用|合并这些唯一值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框:

gene    gene_name   source  chromosome  details
1       a           A           2       01; xyz
1       a           A           2       02; ijk
2       b           B           3       03; efg
2       b           C           3       03; efg
3       c           D           4       04; lmn
3       c           D           4       05; opq
3       c           D           4       06; rst
4       NA          10          6       NA
4       NA          11          6       NA

我要获得以下输出:

gene    gene_name   source  chromosome  details
1       a           A       2           01; xyz | 02;ijk
2       b           B, C    3           03; efg
3       c           D       4           04; lmn | 05; opq | 06; rst
4       NA          10, 11  6           NA | NA

我试图以不同的方式使用aggregate()和group_by(),但没有得到

I have tried to use aggregate() and group_by() in different ways, but did not get it.

请指导。

谢谢。

推荐答案

这应该有效:

df %>%
  group_by(gene, gene_name, source, chromosome) %>%
  summarise(details = paste(details, collapse = " | "))

我在虹膜上运行以下内容,得到的结果与您所描述的相似

I ran the below on iris and got a result similar to as you described

iris %>%
  group_by(Sepal.Length, Sepal.Width, Petal.Length, Species) %>%
  summarise(Petal.Width = paste(Petal.Width, collapse = " | "))

这篇关于R:如何根据单个列中的唯一值合并来自多个列的重复行,并用|合并这些唯一值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆