Superseded

separate() has been superseded by separate_wider_delim().

Separates a single column into multiple columns using a user supplied separator or regex.

If a separator is not supplied one will be automatically detected.

Note: Using automatic detection or regex will be slower than simple separators such as "," or ".".

separate(
  .df,
  col,
  into,
  sep = "[^[:alnum:]]+",
  remove = TRUE,
  convert = FALSE,
  ...
)

Arguments

.df

A data frame

col

The column to split into multiple columns

into

New column names to split into. A character vector. Use NA to omit the variable in the output.

sep

Separator to split on. Can be specified or detected automatically

remove

If TRUE, remove the input column from the output data.table

convert

TRUE calls type.convert() with as.is = TRUE on new columns

...

Arguments passed on to methods

Examples

df <- data.table(x = c("a", "a.b", "a.b", NA))

# "sep" can be automatically detected (slower)
df %>%
  separate(x, into = c("c1", "c2"))
#> # A tidytable: 4 × 2
#>   c1    c2   
#>   <chr> <chr>
#> 1 a     NA   
#> 2 a     b    
#> 3 a     b    
#> 4 NA    NA   

# Faster if "sep" is provided
df %>%
  separate(x, into = c("c1", "c2"), sep = ".")
#> # A tidytable: 4 × 2
#>   c1    c2   
#>   <chr> <chr>
#> 1 a     NA   
#> 2 a     b    
#> 3 a     b    
#> 4 NA    NA