一旦 dplyr 按另一个变量分组,如何为一个变量组合多个文本条目

How to combine multiple text entries for a variable once dplyr has grouped by another variable

对于数百件事,我的数据框包含数十名计时员的每日文本条目。并非每个计时员每天都为每件事输入时间。文本条目可以是任意长度。每个事项的条目都是针对在不同日期完成的工作(但出于我的目的,找出文本的可读性度量,日期并不重要)。我想做的是将每件事的所有文本条目组合起来。

这是一个玩具数据集及其外观:

> dput(df)
structure(list(Matter = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
3L, 4L, 4L), .Label = c("MatterA", "MatterB", "MatterC", "MatterD"
), class = "factor"), Timekeeper = structure(c(1L, 2L, 3L, 4L, 
2L, 3L, 1L, 1L, 3L, 4L), .Label = c("Alpha", "Baker", "Charlie", 
"Delta"), class = "factor"), Text = structure(c(5L, 8L, 1L, 3L, 
7L, 6L, 9L, 2L, 10L, 4L), .Label = c("all", "all we have", "good men to come to", 
"in these times that try men's souls", "Now is", "of", "the aid", 
"the time for", "their country since", "to fear is fear itself"
), class = "factor")), class = "data.frame", row.names = c(NA, 
-10L))

Dplyr 按事件对时间记录进行分组,但我很困惑如何将每个事件的文本条目组合起来,以便结果沿着这些方向排列——为一个事件收集的所有文本:

1   MatterA Now is the time for all good men to come to
5   MatterB the aid of their country since
8   MatterC all we have
9   MatterD to fear is fear itself in these times that try men's souls

dplyr::mutate() 不适用于各种串联函数:

textCombined <- df %>% group_by(Matter) %>% mutate(ComboText = str_c(Text))
textCombined2 <- df %>% group_by(Matter) %>% mutate(ComboText = paste(Text))
textCombined3 <- df %>% group_by(Matter) %>% mutate(ComboText = c(Text)) # creates numbers

也许循环可以完成这项工作,如 "while the matter stays the same, combine the text" 但我不知道如何写。或者 dplyr 可能有条件变异,如 "mutate(while the matter stays the same, combine the text)."

感谢您的帮助。

您好,您可以使用 group by 并使用 paste 进行总结,

> df %>% group_by(Matter) %>% summarise(line= paste(Text, collapse = " "))


# A tibble: 4 x 2
#  Matter  line                                                      
#  <fct>   <chr>                                                     
#1 MatterA Now is the time for all good men to come to               
#2 MatterB the aid of their country since                            
#3 MatterC all we have                                               
#4 MatterD to fear is fear itself in these times that try men's souls