R粘贴属于相同id的数据帧行

R粘贴属于相同id的数据帧行,r,paste,R,Paste,我正在尝试将来自同一用户的文本数据粘贴到一起,该用户当前按名称组织在不同的行中: df <- read.table(header = TRUE, text = 'name text "katy" "tomorrow I go" "lauren" "and computing" "katy" "to the store" "stephanie" "foo and foos"') df我们可以使用data.table或dplyr或aggregate粘贴按“名称”分组的“文本”列。使用data

我正在尝试将来自同一用户的文本数据粘贴到一起,该用户当前按名称组织在不同的行中:

df <- read.table(header = TRUE, text = 'name text
"katy" "tomorrow I go"
"lauren" "and computing"
"katy" "to the store"
"stephanie" "foo and foos"')

df我们可以使用
data.table
dplyr
aggregate
粘贴按“名称”分组的“文本”列。使用
data.table
,在执行此操作之前,我们将“data.frame”转换为“data.table”(
setDT(df)

library(data.table)
setDT(df)[, list(text=paste(text, collapse=' ')), by = name]

使用
dplyr

library(dplyr)
df %>%
   group_by(name) %>%
   summarise(text=paste(text, collapse=' '))

或使用
base R

aggregate(text~name, df, FUN= paste, collapse=' ') 
aggregate(text~name, df, FUN= paste, collapse=' ')