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)
x | [ |
---|---|
split.col | [ |
split | [ |
names | [ |
types | [ |
keep | [ |
Modified data frame.
Other data frame helpers:
df_add_category()
,
df_add_constant_columns()
,
df_rows_to_list()
,
df_split_col()
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.60076089df_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#> 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.60076089df_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