3

This is for people using stringr in R.

I want to split a name into first and trim any stray spaces.

> str_trim(str_split(c("John Smith"),"\\s+"))
[1] "c(\"John\", \"Smith\")"

Where are all the escaped "s coming from?

I was expecting c("John","Smith")

PKumar
  • 10,971
  • 6
  • 37
  • 52
  • 1
    while i can't answer the question, I'm quite certain it has something to do with the fact that `str_split` ( without simplify) returns a list and str_trim handles lists in this ...weird way, for the lack of a better word. The underlying methods from `stringi` package also do the same. – R.S. Jul 15 '17 at 05:52
  • If you `str_split` on `"\\s+"` like you do, you'll never actually get any "stray spaces". They all get gobbled up as separators. See `str_split(" John Smythe Smith ","\\s+")` for example. So the `str_trim` is pointless. – Spacedman Jul 15 '17 at 07:51

2 Answers2

2

Use this, you are using str_trim in a wrong way on a list output of str_split:

library("stringr")
str_trim(str_split("John Smith","\\s")[[1]])

Output:

> str_trim(str_split("John Smith","\\s")[[1]])
[1] "John"  "Smith"
PKumar
  • 10,971
  • 6
  • 37
  • 52
  • 1
    Many thanks. Having looked at the answers, I understand how to use the functions better ... and probably need to put the trim inside the split. res <- str_split(str_trim(c(" first last "), "\\s+")[[1]] – GeoffRussell Jul 16 '17 at 00:13
0

You can also use unlist to produce a vector of strings.

library("stringr")
unlist(str_split(c("John Smith"),"\\s+"))
HNSKD
  • 1,614
  • 2
  • 14
  • 25