mike805 mike805 - 13 days ago 8
R Question

Splitting conjoining words in R

I am trying to split string that is structured like:

string <- c("ThisThat","OneTwo","LeftRight","RightHere")


and turn it into:

>string
[1] "This That" "One Two" "Left Right" "Right Here"


I have tried numerous functions in the 'stringr' and 'stringi' package. Any help?

Answer

does this work for you?

gsub("([A-Z])", " \\1", string) %>% trimws()
[1] "This That"  "One Two"    "Left Right" "Right Here"