extract {tidyr} | R Documentation |
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.
extract(data, col, into, regex = "([[:alnum:]]+)", remove = TRUE, convert = FALSE, ...)
data |
A data frame. |
col |
Column name or position. This is passed to
This argument is passed by expression and supports quasiquotation (you can unquote column names or column positions). |
into |
Names of new variables to create as character vector. |
regex |
a regular expression used to extract the desired values.
The should be one group (defined by |
remove |
If |
convert |
If |
... |
Other arguments passed on to |
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]+)")