R:在同一数据表中合并行,连接某些列

时间:2012-07-06 15:15:58

标签: r merge concatenation

我在R中有我的数据表。我想合并具有相同customerID的行,然后连接其他合并列的元素。

我想离开这个:

   title  author customerID
1 title1 author1          1
2 title2 author2          2
3 title3 author3          1

到此:

           title           author Group.1
1 title1, title3 author1, author3       1
2         title2          author2       2

2 个答案:

答案 0 :(得分: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

或者,只需确保在创建数据框时添加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

答案 1 :(得分:2)

也许不是最好的解决方案,但很容易理解:

df <- data.frame(author=LETTERS[1:5], title=LETTERS[1:5], id=c(1, 2, 1, 2, 3), stringsAsFactors=FALSE)

uniqueIds <- unique(df$id)

mergedDf <- df[1:length(uniqueIds),]

for (i in seq(along=uniqueIds)) {
    mergedDf[i, "id"] <- uniqueIds[i]
    mergedDf[i, "author"] <- paste(df[df$id == uniqueIds[i], "author"], collapse=",")
    mergedDf[i, "title"] <- paste(df[df$id == uniqueIds[i], "title"], collapse=",")
}

mergedDf
#  author title id
#1    A,C   A,C  1
#2    B,D   B,D  2
#3      E     E  3