一旦dplyr被另一个变量分组后,如何为一个变量组合多个文本条目[重复]

问题描述 投票:0回答:1

关于数百个问题,我的数据框每天都有数十位计时员输入文本。并非每个计时员每天都会为每个问题输入时间。文本输入可以是任何长度。每个问题的条目都是在不同的一天完成的工作(但出于我的目的,找出文本的可读性度量标准,日期并不重要)。我想为每件事合并所有的文本条目。

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

> 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

也许循环就可以完成任务,例如“在事情保持不变的情况下,合并文本”,但是我不知道该怎么写。或dplyr可能有条件的变异,例如“变异(当事情保持不变时,合并文本)。”

谢谢您的帮助。

r dplyr text-mining
1个回答
0
投票

您好,您可以使用分组依据并粘贴摘要,

> 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



© www.soinside.com 2019 - 2024. All rights reserved.