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?
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With