Har*_*mer 10 merge r concatenation
我在R中有我的数据表.我想合并具有相同的行customerID,然后连接其他合并列的元素.
我想离开这个:
title author customerID
1 title1 author1 1
2 title2 author2 2
3 title3 author3 1
Run Code Online (Sandbox Code Playgroud)
对此:
title author Group.1
1 title1, title3 author1, author3 1
2 title2 author2 2
Run Code Online (Sandbox Code Playgroud)
A5C*_*2T1 13
该aggregate功能应该可以帮助您找到解决方案:
dat = data.frame(title = c("title1", "title2", "title3"),
author = c("author1", "author2", "author3"),
customerID = c(1, 2, 1))
aggregate(dat[-3], by=list(dat$customerID), c)
# Group.1 title author
# 1 1 1, 3 1, 3
# 2 2 2 2
Run Code Online (Sandbox Code Playgroud)
或者,只需确保stringsAsFactors = FALSE在创建数据框时添加,然后就可以了.如果您的数据已经被分解,您可以先使用类似的dat[c(1, 2)] = apply(dat[-3], 2, as.character)方法将它们转换为字符,然后:
aggregate(dat[-3], by=list(dat$customerID), c)
# Group.1 title author
# 1 1 title1, title3 author1, author3
# 2 2 title2 author2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11755 次 |
| 最近记录: |