我有以下数据框,在页面和段落列中有许多不同的值
df <- read.table(text="page passage person index text
1 123 A 1 hello
1 123 A 2 my
1 123 A 3 name
1 123 A 4 is
1 123 A 5 guy
1 124 B 1 well
1 124 B 2 hello
1 124 B 3 guy",header=T,stringsAsFactors=F)
我想根据这些列拼接文本列的内容,使其看起来像这样
1 123 A 1 hello my name is guy
1 123 A 2 hello my name is guy
1 123 A 3 hello my name is guy
1 123 A 4 hello my name is guy
1 123 A 5 hello my name is guy
1 124 B 1 well hello guy
1 124 B 2 well hello guy
1 124 B 3 well hello guy
最佳答案
在分组函数中使用 paste
with collapse:
基础R
df$text <- ave(df$text, df$person, FUN = function(x) paste(x, collapse = " "))
dplyr
library(dplyr)
df %>%
group_by(person) %>%
mutate(text = paste(text, collapse=" "))
数据表
setDT(df)[, text := paste(text, collapse = " "), person]
输出
page passage person index text
<int> <int> <chr> <int> <chr>
1 1 123 A 1 hello my name is guy
2 1 123 A 2 hello my name is guy
3 1 123 A 3 hello my name is guy
4 1 123 A 4 hello my name is guy
5 1 123 A 5 hello my name is guy
6 1 124 B 1 well hello guy
7 1 124 B 2 well hello guy
8 1 124 B 3 well hello guy
关于r - 使用 dplyr 根据其他列的值更改列的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71068315/