If you want to use quanteda for this, you can process convert this to a corpus, and then process it via two corpus_segment()
calls, one to get the text before , and the second to then just select the text after . Then you can re-group the text using texts(x, groups = docid())
, specifying the spacer = ", "
.
Here's how, with your desired output:
library("quanteda")
## Package version: 2.1.1
df <- data.frame(
id = c(1, 2),
text = c("something here <h1>my text</h1> also <h1>Keep it</h1>", "<h1>title</h1> another here")
)
charvec <- corpus(df, docid_field = "id") %>%
corpus_segment("</h1>", pattern_position = "after") %>%
corpus_segment("<h1>", pattern_position = "before") %>%
texts(groups = docid(.), spacer = ", ")
Then to convert this into the data.frame
that you want:
data.frame(text = charvec, id = names(charvec))
## text id
## 1 my text, Keep it 1
## 2 title 2