使用dplyr根据其他列的值更改列内容

3
我有一个包含许多不同值的数据框,其中包括页面和段落列。
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
1个回答

5

使用 collapse 参数和一个分组函数,结合 paste 函数来实现:

基础 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=" "))

data.table

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接