Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

r - How to aggregate texts and metadata from subunits to larger units?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...