df_explode: Split variable (column) into multiple.

View source: R/df_explode.R

df_explodeR Documentation

Split variable (column) into multiple.

Description

Split/explode a data frame character column into multiple columns. This is done by splitting the string by one or multiple delimiters. The split results are wrapped in a data.frame and “cbinded” to x. Optionally, one can pass column names and/or types for the parts. E.g. types="cir" converts the first part into character, the second into integer and the third into real/double.

Usage

df_explode(x, split.col, split, names = NULL, types = NULL, keep = FALSE)

Arguments

x

[data.frame]
Data frame.

split.col

[character(1)]
Character name of column used for splitting.

split

[character(1)]
See parameter split of strsplit.

names

[character]
Optional column names. If NULL, the column names are V1 to VN where N is the number of split parts.

types

[character(1)]
Character string where the i-th character specifies the atomic data type of the i-th split part. Possible characters are “c” (character), “f” (factor), “i” (integer), “l” (logical) or “n”, “d” or “r” for numeric/double/real. Default is NULL. In this case all split parts are characters.

keep

[logical(1)]
Single logical value indicating whether column split.col should be kept or dropped. Default is FALSE.

Value

Modified data frame.

See Also

Other data frame helpers: df_add_category(), df_add_constant_columns(), df_rows_to_list(), df_split_col()

Examples

x = data.frame(a = c("x-y-1", "a-b-2", "c-d-3"), b = 1:3, c = runif(3))
df_explode(x, split.col = "a", split = "-")
df_explode(x, split.col = "a", split = "-", keep = TRUE)
df_explode(x, split.col = "a", split = "-", names = c("A", "B", "C"))
df_explode(x, split.col = "a", split = "-", types = "cci")

jakobbossek/re documentation built on Nov. 15, 2024, 1:42 a.m.