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

有帮助吗?

解决方案

You can match the following:

great\\s*(?!$)

and replace with:

'great '

DEMO

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top