I have the result of a select_multiple question stored in a list. That comes from a dataset collected with OpenDataKit
example <- list("a", c("b", "c"), c("d", "e", "f"), c(""))
In the example below for the record #4
, there were no answers (meaning NA
for all options).
I need to create a data frame from this list where each options from the select multiple would become a new variable. Each element of the list is de facto not of the same length.
The result should look like this:
variable | a b c d e f
row1 | 1 0 0 0 0 0
row2 | 0 1 1 0 0 0
row3 | 0 0 0 1 1 1
row4 | <NA> <NA><NA> <NA><NA> <NA>
I have found options with stri_list2matrix
but that does not provide the expected results.
I tried as well
df <-data.frame( lNames <- rep(names(example), lapply(example, length)),
lVal <- unlist(example))
and got the same
arguments imply differing number of rows
Please help!
Thanks
You could use setNames
, stack
and dcast
for that:
example <- list("a", c("b", "c"), c("d", "e", "f"), c(""))
example <- setNames(example, seq_along(example))
ex2 <- stack(example)
ex2[ex2$values=='','values'] <- NA
library(reshape2)
dcast(ex2, ind ~ values, fun.aggregate = length)
This will result in:
ind a b c d e f NA
1 1 1 0 0 0 0 0 0
2 2 0 1 1 0 0 0 0
3 3 0 0 0 1 1 1 0
4 4 0 0 0 0 0 0 1