tidyr (version 0.3.1)

separate: Separate one column into multiple columns.

Description

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

Usage

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

Arguments

data
A data frame.
col
Bare column name.
into
Names of new variables to create as character vector.
sep
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 st

remove
If TRUE, remove input column from output data frame.
convert
If TRUE, will run type.convert with as.is = TRUE on new columns. This is useful if the component columns are integer, numeric or logical.
extra
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 waring and drop extra values.
  • "drop": drop any extra values without a wa

fill
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 waring and fill from the right
  • "right": fill with missing values on th

...
Other arguments passed on to strsplit to control how the regular expression is processed.

Examples

Run this code
library(dplyr)
df <- data.frame(x = c(NA, "a.b", "a.d", "b.c"))
df %>% separate(x, c("A", "B"))

# If every row doesn't split into the same number of pieces, use
# the extra and file 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")

Run the code above in your browser using DataCamp Workspace