0

I am (very!) new to using R, and am facing a problem which is probably (very!) easy to solve. I'm seeking to separate a column of characters, and - while it's done automatically for me without specifying the separation character - I'd like to be able to specify the character to avoid ambiguity.

Here is an example:

economists <- tibble(name = c("Adam.Smith", "Paul.Samuelson", "Milton.Friedman"))
economists %>%
  separate(name, c("first_name", "last_name"), sep=".")

This produces this:

# A tibble: 3 x 2
  first_name last_name
  <chr>      <chr>    
1 ""         ""       
2 ""         ""       
3 ""         ""       
Warning message:
Expected 2 pieces. Additional pieces discarded in 3 rows [1, 2, 3]. 

I'd instead like it to produce this:

# A tibble: 3 x 2
  first_name last_name
  <chr>      <chr>    
1 Adam       Smith    
2 Paul       Samuelson
3 Milton     Friedman 

Which it does when I don't specify that the column is separated with a period.

What am I missing?

bee
  • 43
  • 7
  • Hi, the "sep" argument uses regular expressions. The "." means "anything", so you need to escape the regex using" \\". – AndS. Jul 20 '20 at 16:58
  • @AndS. Of course! Thank you - very much appreciated. – bee Jul 20 '20 at 17:07

1 Answers1

2

Try this:

economists <- tibble(name = c("Adam.Smith", "Paul.Samuelson", "Milton.Friedman"))
economists %>%
    separate(name, c("first_name", "last_name"), sep="\\.")

# A tibble: 3 x 2
  first_name last_name
  <chr>      <chr>    
1 Adam       Smith    
2 Paul       Samuelson
3 Milton     Friedman 
Duck
  • 39,058
  • 13
  • 42
  • 84