Here's a fun one. I'm trying to do exactly what this post is doing. That is, repeating and grouping words.
The catch with this question is that I'd like to do it purely with stringr
's word()
function with a paste0
wrapper. Take the following sentence
sentence <- "Jane saw a cat and then Jane sat down."
The exact result would be
[1] "Jane saw, saw a, a cat, cat and, and then, then Jane, Jane sat, sat down."
I've gotten this far, but word()
leaves an extra ""
at the end of this string, likely due to the way I've written my code in word()
because it doesn't otherwise leave an empty string.
> library(stringr)
> len <- length(strsplit(sentence, " ")[[1]])
> paste0(word(sentence, c(1, 2:len), c(2, 3:len)), collapse = ", ")
[1] "Jane saw, saw a, a cat, cat and, and then, then Jane, Jane sat, sat down., "
Can this be done without the trailing ", "
using only the word()
function?