dt_separate {tidyfast} | R Documentation |
Separate columns with data.table
Description
Separates a column of data into others, by splitting based a separator or regular expression
Usage
dt_separate(
dt_,
col,
into,
sep = ".",
remove = TRUE,
fill = NA,
fixed = TRUE,
immutable = TRUE,
dev = FALSE,
...
)
Arguments
dt_ |
the data table (or if not a data.table then it is coerced with as.data.table) |
col |
the column to separate |
into |
the names of the new columns created from splitting |
sep |
the regular expression stating how |
remove |
should |
fill |
if empty, fill is inserted. Default is |
fixed |
logical. If TRUE match split exactly, otherwise use regular expressions. Has priority over perl. |
immutable |
If |
dev |
If |
... |
arguments passed to |
Value
A data.table with a column split into multiple columns.
Examples
library(data.table)
d <- data.table(
x = c("A.B", "A", "B", "B.A"),
y = 1:4
)
# defaults
dt_separate(d, x, c("c1", "c2"))
# can keep the original column with `remove = FALSE`
dt_separate(d, x, c("c1", "c2"), remove = FALSE)
# need to assign when `immutable = TRUE`
separated <- dt_separate(d, x, c("c1", "c2"), immutable = TRUE)
separated
# don't need to assign when `immutable = FALSE` (default)
dt_separate(d, x, c("c1", "c2"), immutable = FALSE)
d