Rob
Rob

Reputation: 1042

Insert spaces after certain words in R with gsub

How do you add spaces behind certain words in R if they are not the last word?

So for the word "great" it would change:

"whatagreatday" => "whatagreat day" (space inserted)

"what a great day" => "what a great day" (no change, space there already)

"I feel great" => "I feel great" (dont insert space at end)

Thought it should be relatively straight forward using gsub, but couldnt get the similar Insert space before some character if space does not exist to work in R (despite adding extra backslashes).

Thanks

Upvotes: 0

Views: 2048

Answers (1)

sshashank124
sshashank124

Reputation: 32189

You can match the following:

great\\s*(?!$)

and replace with:

'great '

DEMO

Upvotes: 2

Related Questions