I want to split the following string
"ATextIWantToDisplayWithSpaces"
like this
A Text I Want To Display With Spaces.
I tried this code in R
strsplit(x="ATextIWantToDisplayWithSpaces", split=[:upper:])
which produces this error
Error: unexpected '[' in "strsplit(x="ATextIWantToDisplayWithSpaces", split=["
Any help will be highly appreciated. Thanks
An answer to your specific question ("how do I split on uppercase letters"?) is
but @Ramnath's answer is what you actually want.
strsplit
throws away the characters on which it splits. ThesplitByPattern
function fromR.utils
is closer, but it still won't return the results in the most convenient form for you.Just do this. It works by (a) locating an upper case letter, (b) capturing it in a group and (c) replacing it with the same with a space preceding it.
I know this is an old one, but I adapted the solution above to one I had where I needed to split the values of a column in a data frame by upper case and then only keep the second element. This solution uses dplyr and purrr: