首页 > 解决方案 > 如何将属于一组的元素排列在一行中

问题描述

我的数据是这样的-

p1  x1
p2  x2
p1  x3
p7  x4
p5  x5
p2  x6
p5  x7
p7  x8
p1  x9
p2  x10

我想这样安排——

p1  x1,x2,x9
p2  x2,x6,x10
p7  x4,x8
p5  x5,x7

如何使用 R/命令行或 Excel 执行此操作?

标签: rexcelcommand-line

解决方案


我们可以用aggregate

aggregate(col2 ~ col1, df1, toString)

如果有重复的元素,获取unique行并执行aggregate

aggregate(col2 ~ col1, unique(df1), toString)

推荐阅读