Search code examples
rdataframetidyverseseparatortibble

Problems with the sep argument to the separate() command in library(tidyverse)


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?


Solution

  • 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