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.

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), dQuote'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

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 = "-")
#> explode1 explode2 explode3 b c #> 1 x y 1 1 0.08075014 #> 2 a b 2 2 0.83433304 #> 3 c d 3 3 0.60076089
df_explode(x, split.col = "a", split = "-", keep = TRUE)
#> explode1 explode2 explode3 a b c #> 1 x y 1 x-y-1 1 0.08075014 #> 2 a b 2 a-b-2 2 0.83433304 #> 3 c d 3 c-d-3 3 0.60076089
df_explode(x, split.col = "a", split = "-", names = c("A", "B", "C"))
#> A B C b c #> 1 x y 1 1 0.08075014 #> 2 a b 2 2 0.83433304 #> 3 c d 3 3 0.60076089
df_explode(x, split.col = "a", split = "-", types = "cci")
#> explode1 explode2 explode3 b c #> 1 x y 1 1 0.08075014 #> 2 a b 2 2 0.83433304 #> 3 c d 3 3 0.60076089