Splitting String based on letters case

2020-02-01 08:12发布

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

标签: r
3条回答
淡お忘
2楼-- · 2020-02-01 08:30

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-01 08:48

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)
查看更多
▲ chillily
4楼-- · 2020-02-01 08:50

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())
查看更多
登录 后发表回答