Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
dplyr被另一个变量分组后,如何组合一个变量的多个文本项_R_Dplyr_Text Mining - Fatal编程技术网

dplyr被另一个变量分组后,如何组合一个变量的多个文本项

dplyr被另一个变量分组后,如何组合一个变量的多个文本项,r,dplyr,text-mining,R,Dplyr,Text Mining,对于数百件事情,我的数据框每天都有数十名计时员输入的文本。并不是每个计时员每天都为每一件事输入时间。文本条目可以是任意长度。一件事的每一个条目都是在不同的一天完成的(但就我而言,找出文本的可读性指标,日期并不重要)。我想做的是为每件事合并所有的文本条目 这是一个玩具数据集及其外观: > dput(df) structure(list(Matter = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 4L, 4L), .Label = c("Matt

对于数百件事情,我的数据框每天都有数十名计时员输入的文本。并不是每个计时员每天都为每一件事输入时间。文本条目可以是任意长度。一件事的每一个条目都是在不同的一天完成的(但就我而言,找出文本的可读性指标,日期并不重要)。我想做的是为每件事合并所有的文本条目

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

> 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
textCombined%group\u by(Matter)%%>%mutate(ComboText=str\u c(Text))
textCombined2%groupby(Matter)%%>%mutate(ComboText=paste(Text))
textCombined3%groupby(Matter)%>%mutate(ComboText=c(Text))#创建数字
也许一个循环可以完成这项工作,比如“当事情保持不变时,合并文本”,但我不知道怎么写。或者,
dplyr
有一个条件mutate,如“mutate(当事情保持不变时,合并文本)”


谢谢您的帮助。

您好,您可以使用“分组依据”和“粘贴摘要”

> 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



>df%>%group\u by(Matter)%>%summary(line=paste(Text,collapse=”“))
#一个tibble:4x2
#物质线
#                                                          
#1马特拉现在是所有好人都来的时候了
#2.自那以后,他们一直在寻求国家的援助
#3.我们所拥有的一切
#在这个考验人的灵魂的时代,恐惧最重要的是恐惧本身
df%>%groupby(Matter)%%>%summary(ComboText=paste0(Text,collapse=”“)