Fre*_*red 3 grouping r string-concatenation dataframe dplyr
我有以下数据框,在页面和段落列中有许多不同的值
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)
Run Code Online (Sandbox Code Playgroud)
我想根据这些列连接文本列的内容,使其看起来像这样
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
Run Code Online (Sandbox Code Playgroud)
paste在分组函数中与折叠一起使用:
df$text <- ave(df$text, df$person, FUN = function(x) paste(x, collapse = " "))
Run Code Online (Sandbox Code Playgroud)
library(dplyr)
df %>%
group_by(person) %>%
mutate(text = paste(text, collapse=" "))
Run Code Online (Sandbox Code Playgroud)
setDT(df)[, text := paste(text, collapse = " "), person]
Run Code Online (Sandbox Code Playgroud)
输出
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
Run Code Online (Sandbox Code Playgroud)