[英]R: How to combine duplicated rows from multiple columns based on unique values in a single column and merge those unique values by |?
[英]Is there a way to sort and combine values from multiple columns into a single column using R?
有没有一种方法可以使用R对多个列中的值进行排序并将其合并为一个列?
例如:如果我有如下数据框:
[1] [2] [3]
[1] 2 bb cat
[2] 3 cc bat
[3] 5 xx sat
我想编写代码来创建一个新列,该列对前三个值进行排序,然后将它们组合成一个逗号分隔的值
预期产量:
[1] [2] [3] [4]
[1] 2 bb cat 2,bb,cat
[2] 3 cc bat 3,bat,cc
[3] 5 xx sat 5,sat,xx
您可以执行以下操作:
m<-(transpose(data.frame(apply(df, 1, sort)))) ## Sort the dataframe, transpose it back to normal
m<-data.frame(d=m$V1, a=m$V2, t=m$V3) #Rename the columns
cols <- c('d','a','t') #select the columns you want to paste together
m$x <- do.call(paste, c(m[cols], sep=",")) #create the new column with pasted columns
m
d a t x
1 2 bb cat 2,bb,cat
2 3 bat cc 3,bat,cc
3 5 sat xx 5,sat,xx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.