I have a dataframe of paragraphs from within speeches. I would like to combine the row-level data from paragraphs to speech level, concatenating the paragraph text and keeping the metadata. For example, my data looks like this, where index is the value I want to aggregate on:
text <- c("cat", "dog", "rabbit", "bird")
pet <- c("y", "y", "n", "n")
index <- c(1, 1, 2, 2)
para <- seq(1:4)
orig <- data.frame(text, pet, index, para)
And I would like to transform it to look like this:
ideal <- c("cat dog", "rabbit bird")
ideal_pet <- c("y", "n")
index <- seq(1:2)
ideal <- data.frame(ideal, ideal_pet, index)
If I group_by
I won't be able to concatenate the text strings together- my sense is that a loop over the rows would be the way to go?
question from:
https://stackoverflow.com/questions/65943031/how-to-aggregate-texts-and-metadata-from-subunits-to-larger-units 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…