Given either regular expression, separate_dt() turns a single character column into two columns.

separate_dt(
  .data,
  separated_colname,
  into,
  sep = "[^[:alnum:]]+",
  remove = TRUE
)

Arguments

.data

A data frame.

separated_colname

Column to be separated, can be a character or alias.

into

Character vector of length 2.

sep

Separator between columns.

remove

If TRUE, remove input column from output data frame.

See also

Examples

df <- data.frame(x = c(NA, "a.b", "a.d", "b.c"))
df %>% separate_dt(x, c("A", "B"))
#>         A      B
#>    <char> <char>
#> 1:   <NA>   <NA>
#> 2:      a      b
#> 3:      a      d
#> 4:      b      c
# equals to
df %>% separate_dt("x", c("A", "B"))
#>         A      B
#>    <char> <char>
#> 1:   <NA>   <NA>
#> 2:      a      b
#> 3:      a      d
#> 4:      b      c

# If you just want the second variable:
df %>% separate_dt(x,into = c(NA,"B"))
#>         B
#>    <char>
#> 1:   <NA>
#> 2:      b
#> 3:      d
#> 4:      c