0

For hundreds of matters, my data frame has daily text entries by dozens of timekeepers. Not every timekeeper enters time each day for each matter. Text entries can be any length. Each entry for a matter is for work done on a different day (but for my purposes, figuring out readability measures for the text, dates don't matter). What I would like to do is to combine for each matter all of its text entries.

Here is a toy data set and what it looks like:

> 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 groups the time records by matter, but I am stumped as to how to combine the text entries for each matter so that the result is along these lines -- all text gathered for a matter:

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() does not work with various concatenation functions:

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

Maybe a loop will do the job, as in "while the matter stays the same, combine the text" but I don't know how to write that. Or maybe dplyr has a conditional mutate, as in "mutate(while the matter stays the same, combine the text)."

Thank you for your help.

lawyeR
  • 7,488
  • 5
  • 33
  • 63

1 Answers1

1

Hi you can use group by and summarise with 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



user12256545
  • 2,755
  • 4
  • 14
  • 28