Separate one column into multiple columns.

Given either regular expression or a vector of character positions, separate() turns a single character column into multiple columns.

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

A data frame.


Column name or position. This is passed to tidyselect::vars_pull().

This argument is passed by expression and supports quasiquotation (you can unquote column names or column positions).


Names of new variables to create as character vector. Use NA to omit the variable in the output.


Separator between columns.

If character, is interpreted as a regular expression. The default value is a regular expression that matches any sequence of non-alphanumeric values.

If numeric, interpreted as positions to split at. Positive values start at 1 at the far-left of the string; negative value start at -1 at the far-right of the string. The length of sep should be one less than into.


If TRUE, remove input column from output data frame.


If TRUE, will run type.convert() with = TRUE on new columns. This is useful if the component columns are integer, numeric or logical.


If sep is a character vector, this controls what happens when there are too many pieces. There are three valid options:

  • "warn" (the default): emit a warning and drop extra values.

  • "drop": drop any extra values without a warning.

  • "merge": only splits at most length(into) times


If sep is a character vector, this controls what happens when there are not enough pieces. There are three valid options:

  • "warn" (the default): emit a warning and fill from the right

  • "right": fill with missing values on the right

  • "left": fill with missing values on the left


Additional arguments passed on to methods.

See Also

unite(), the complement, extract() which uses regular expression capturing groups.

  • separate
df <- data.frame(x = c(NA, "a.b", "a.d", "b.c"))
df %>% separate(x, c("A", "B"))

# If you just want the second variable:
df %>% separate(x, c(NA, "B"))

# If every row doesn't split into the same number of pieces, use
# the extra and fill arguments to control what happens
df <- data.frame(x = c("a", "a b", "a b c", NA))
df %>% separate(x, c("a", "b"))
# The same behaviour but no warnings
df %>% separate(x, c("a", "b"), extra = "drop", fill = "right")
# Another option:
df %>% separate(x, c("a", "b"), extra = "merge", fill = "left")

# If only want to split specified number of times use extra = "merge"
df <- data.frame(x = c("x: 123", "y: error: 7"))
df %>% separate(x, c("key", "value"), ": ", extra = "merge")
# }
Documentation reproduced from package tidyr, version 0.8.2, License: MIT + file LICENSE

Community examples at Oct 1, 2019 tidyr v0.8.3

If a period is the separator, must use regex where `sep = "[.]"`. This is helpful if you did `, my_list) %>% rownames_to_column("study_area")` ``` > df <- data.frame(x = c("study area 1.1", "study area 1.2", "study area 1.3", "study area 2.1", "study area 2.2")) > df %>% separate(x, c("study area", NA), sep = "[.]") study area 1 study area 1 2 study area 1 3 study area 1 4 study area 2 5 study area 2 ``` at Dec 8, 2017 tidyr v0.7.2

### Numeric Case library(dplyr) df <- data.frame(x = c(11, 21, 13, 41)) df %>% separate(x, c("A", "B"),sep=1)