Question

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

Was it helpful?

Solution

You can match the following:

great\\s*(?!$)

and replace with:

'great '

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top