MYaseen208
MYaseen208

Reputation: 23898

Splitting String based on letters case

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

Upvotes: 10

Views: 10279

Answers (4)

jpdugo17
jpdugo17

Reputation: 7106

Using stringr

library(stringr)

str_replace_all(
  string =  "ATextIWantToDisplayWithSpaces",
  pattern = "([[:upper:]])",
  replacement = " \\1"
) %>% 
  str_trim()

#[1] "A Text I Want To Display With Spaces"

Upvotes: 1

RTutt
RTutt

Reputation: 11

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())

Upvotes: 1

Ben Bolker
Ben Bolker

Reputation: 226097

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.

Upvotes: 9

Ramnath
Ramnath

Reputation: 55695

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)

Upvotes: 35

Related Questions