Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert string after every Nth word

Tags:

regex

r

I'm trying to edit every String in a row of a dataframe. e.g. this dataframe:

df <- data.frame( words = c("Three can keep a secret, if two of them are dead."))

My goal is to fill in after every third word a "\b", so my dataframe should look like following:

df2 <- data.frame(words = c("Three can keep \b a secret, if \b two of them \b are dead."))

I already tried to use the gsub() and the pattern " \\s{3}", but it doesn't work. How does the correct expression for this operation looks like?

like image 252
Leon Avatar asked Jan 25 '26 12:01

Leon


1 Answers

You have to allow for the letters too. This should get you what you're after

df2 <- within(df1, words <- gsub("(([A-Za-z1-9.,']+\\s){3})","\\1\b ", words))

This might work better if you have lots of punctuation to work around (I'm only looking for . , and ' above:

df2 <- within(df1, words <- gsub("((\\S+\\s){3})","\\1\b ", words))
like image 172
Melissa Key Avatar answered Jan 27 '26 02:01

Melissa Key