How to combine rows based on unique values in R? [duplicate]

三世轮回 提交于 2019-12-20 03:20:00

问题


I'm a pretty beginner at R. I've a CSV file where data is as follows, for example:

ID  Values
820 D1,D2,FE
730 D1,D2,D3,PC,Io,He,Bt,Te,AR,PG
730 DV,GTH,LYT
567 EDR,TYU,EOP,OMN
567 FGH,KIH,IOP

I want to remove the duplicates in ID and append their data into its Values column, like this:

ID  Values
820 D1,D2,FE
730 D1,D2,D3,PC,Io,He,Bt,Te,AR,PG,DV,GTH,LYT
567 EDR,TYU,EOP,OMN,FGH,KIH,IOP

How to achieve this in R?


回答1:


dat <- read.table(text="ID  Values
820 D1,D2,FE
730 D1,D2,D3,PC,Io,He,Bt,Te,AR,PG
730 DV,GTH,LYT
567 EDR,TYU,EOP,OMN
567 FGH,KIH,IOP", header=TRUE)

dat2 <- dat %>% group_by(ID) %>% summarise(val=paste(Values, collapse=","))



回答2:


You can try

library(data.table)
setDT(df1)[, list(Values=paste(Values, collapse=",")) ,ID]

Or using base R

 aggregate(.~ID, df1, paste, collapse=",")


来源:https://stackoverflow.com/questions/30235606/how-to-combine-rows-based-on-unique-values-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!