I have a tibble with a list of words for each row. I want to create a new variable from a function that searches for a keyword and, if it finds the keyword, creates a string composed of the keyword plus-and-minus 3 words.
The code below is close, but, rather than grabbing all three words before and after my keyword, it grabs the single word 3 ahead/behind.
df <- tibble(words = c("it", "was", "the", "best", "of", "times",
"it", "was", "the", "worst", "of", "times"))
df <- df %>% mutate(chunks = ifelse(words=="times",
paste(lag(words, 3),
words,
lead(words, 3), sep = " "),
NA))
The most intuitive solution would be if the lag
function could do something like this: lead(words, 1:3)
but that doesn't work.
Obviously I could pretty quickly do this by hand (paste(lead(words,3), lead(words,2), lead(words,1),...lag(words,3)
), but I'll eventually actually want to be able to grab the keyword plus-and-minus 50 words--too much to hand-code.
Would be ideal if a solution existed in the tidyverse, but any solution would be helpful. Any help would be appreciated.