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=' ')