View source: R/data_separate.R
data_separate | R Documentation |
Separates a single variable into multiple new variables.
data_separate(
data,
select = NULL,
new_columns = NULL,
separator = "[^[:alnum:]]+",
guess_columns = NULL,
merge_multiple = FALSE,
merge_separator = "",
fill = "right",
extra = "drop_right",
convert_na = TRUE,
exclude = NULL,
append = FALSE,
ignore_case = FALSE,
verbose = TRUE,
regex = FALSE,
...
)
data |
A data frame. |
select |
Variables that will be included when performing the required tasks. Can be either
If |
new_columns |
The names of the new columns, as character vector. If
more than one variable was selected (in |
separator |
Separator between columns. Can be a character vector, which is then treated as regular expression, or a numeric vector that indicates at which positions the string values will be split. |
guess_columns |
If |
merge_multiple |
Logical, if |
merge_separator |
Separator string when |
fill |
How to deal with values that return fewer new columns after
splitting? Can be |
extra |
How to deal with values that return too many new columns after
splitting? Can be |
convert_na |
Logical, if |
exclude |
See |
append |
Logical, if |
ignore_case |
Logical, if |
verbose |
Toggle warnings. |
regex |
Logical, if |
... |
Currently not used. |
A data frame with the newly created variable(s), or - when append = TRUE
-
data
including new variables.
data_unite()
# simple case
d <- data.frame(
x = c("1.a.6", "2.b.7", "3.c.8"),
stringsAsFactors = FALSE
)
d
data_separate(d, new_columns = c("a", "b", "c"))
# guess number of columns
d <- data.frame(
x = c("1.a.6", NA, "2.b.6.7", "3.c", "x.y.z"),
stringsAsFactors = FALSE
)
d
data_separate(d, guess_columns = "mode")
data_separate(d, guess_columns = "max")
# drop left-most column
data_separate(d, guess_columns = "mode", extra = "drop_left")
# merge right-most column
data_separate(d, guess_columns = "mode", extra = "merge_right")
# fill columns with fewer values with left-most values
data_separate(d, guess_columns = "mode", fill = "value_left")
# fill and merge
data_separate(
d,
guess_columns = "mode",
fill = "value_left",
extra = "merge_right"
)
# multiple columns to split
d <- data.frame(
x = c("1.a.6", "2.b.7", "3.c.8"),
y = c("x.y.z", "10.11.12", "m.n.o"),
stringsAsFactors = FALSE
)
d
# split two columns, default column names
data_separate(d, guess_columns = "mode")
# split into new named columns, repeating column names
data_separate(d, new_columns = c("a", "b", "c"))
# split selected variable new columns
data_separate(d, select = "y", new_columns = c("a", "b", "c"))
# merge multiple split columns
data_separate(
d,
new_columns = c("a", "b", "c"),
merge_multiple = TRUE
)
# merge multiple split columns
data_separate(
d,
new_columns = c("a", "b", "c"),
merge_multiple = TRUE,
merge_separator = "-"
)
# separate multiple columns, give proper column names
d_sep <- data.frame(
x = c("1.a.6", "2.b.7.d", "3.c.8", "5.j"),
y = c("m.n.99.22", "77.f.g.34", "44.9", NA),
stringsAsFactors = FALSE
)
data_separate(
d_sep,
select = c("x", "y"),
new_columns = list(
x = c("A", "B", "C"), # separate "x" into three columns
y = c("EE", "FF", "GG", "HH") # separate "y" into four columns
),
verbose = FALSE
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.