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

标签 r dataframe dplyr grouping string-concatenation

我有以下数据框,在页面和段落列中有许多不同的值

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/

相关文章:

html - open.connection(x, "rb") 错误 : Couldn't connect to server

r - 如何通过合并 csv 文件创建数据框,然后基于它创建 Shiny 的应用程序?

python Pandas : How to create a binary matrix from column of lists?

python - 在具有 NaN 值的 pandas 数据帧上操作时精度损失

r - 如何只保留唯一行而忽略一列?

R - 如何用 dplyr 中变量的名称替换变量的值

r - 在双轴图上绘制不同级别的零线

r - 如何在 R 中为绘图热图生成自定义色标

r - 计算绘图上文本的边界框,包括基线以下的文本

r - 如何根据条件语句和 dplyr 创建新列?