Learn R Programming

tidyr (version 0.6.1)

extract: Extract one column into multiple columns.

Description

Given a regular expression with capturing groups, extract() turns each group into a new column. If the groups don't match, or the input is NA, the output will be NA.

Usage

extract(data, col, into, regex = "([[:alnum:]]+)", remove = TRUE, convert = FALSE, ...)

Arguments

data
A data frame.
col
Bare column name.
into
Names of new variables to create as character vector.
regex
a regular expression used to extract the desired values.
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.
...
Other arguments passed on to regexec to control how the regular expression is processed.

See Also

extract_ for a version that uses regular evaluation and is suitable for programming with.

Examples

Run this code
library(dplyr)
df <- data.frame(x = c(NA, "a-b", "a-d", "b-c", "d-e"))
df %>% extract(x, "A")
df %>% extract(x, c("A", "B"), "([[:alnum:]]+)-([[:alnum:]]+)")

# If no match, NA:
df %>% extract(x, c("A", "B"), "([a-d]+)-([a-d]+)")

Run the code above in your browser using DataLab