Letter-based line breaks

I want to split the next line

"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 would be greatly appreciated. Thanks

+7
source share
2 answers

Just do it. It works by (a) placing an uppercase letter, (b) capturing it in a group, and (c) replacing it with the same space in front of it.

 gsub('([[:upper:]])', ' \\1', x) 
+23
source

The answer to your specific question ("how to divide by capital letters"?) Is

 strsplit(x="ATextIWantToDisplayWithSpaces", split="[[:upper:]]") 

but @Ramnath's answer is what you really want. strsplit throws out the characters it is split into. The splitByPattern function from R.utils closer, but it still will not return the results in the form that is most convenient for you.

+7
source

All Articles