Converts list columns into separate columns or into a long form.
col_flatten(indt, cols, drop = FALSE) col_flattenLong(indt, cols)
indt | The input |
---|---|
cols | Character vector containing the names of list columns |
drop | Logical. Should the list columns be dropped from the original
|
A data.table
.
http://stackoverflow.com/q/34206003/1270695
Ananda Mahto
df <- structure( list(CAT = structure(1:2, .Label = c("A", "B"), class = "factor"), COUNT = list(1:3, 4:5), TREAT = list(c("Treat-a", "Treat-b"), c("Treat-c", "Treat-d", "Treat-e"))), .Names = c("CAT", "COUNT", "TREAT"), row.names = c(NA, -2L), class = "data.frame") col_flatten(df, c("COUNT", "TREAT"), TRUE)#> CAT COUNT_1 COUNT_2 COUNT_3 TREAT_1 TREAT_2 TREAT_3 #> 1: A 1 2 3 Treat-a Treat-b <NA> #> 2: B 4 5 NA Treat-c Treat-d Treat-e#> CAT variable COUNT TREAT #> 1: A 1 1 Treat-a #> 2: A 2 2 Treat-b #> 3: A 3 3 <NA> #> 4: B 1 4 Treat-c #> 5: B 2 5 Treat-d #> 6: B 3 NA Treat-e