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
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.
gsub('([[:upper:]])', ' \\1', x)
An answer to your specific question ("how do I split on uppercase letters"?) is
strsplit(x="ATextIWantToDisplayWithSpaces", split="[[:upper:]]")
but @Ramnath's answer is what you actually want. strsplit
throws away the characters on which it splits. The splitByPattern
function from R.utils
is closer, but it still won't return the results in the most convenient form for you.
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:
df %>% mutate(stringvar= map(strsplit(stringvar, "(?!^)(?=[[:upper:]])", perl=T),~.x[2]) %>% unlist())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With