1

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

user3148607
  • 191
  • 11

1 Answers1

1

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
h3rm4n
  • 4,126
  • 15
  • 21
  • Thanks for the quick answer. When I run the code, I get an error: > ex2 <- stack(example, names(example)) Error in if (drop) { : argument is not interpretable as logical In addition: Warning message: In if (drop) { : the condition has length > 1 and only the first element will be used – user3148607 May 28 '17 at 23:31
  • Nice!! Can anyone explain why the above answer , which uses setNames, works but directly assigning `ex2 <- stack(example, seq_along(example))` produces message "Error in rep.int(names(x), lengths(x)) : invalid 'times' value" – Andrew Lavers May 28 '17 at 23:36
  • @epi99 - because it's superfluous - the names are already on `example` from the previous line and are then used to name the `stack`. It could have just been `stack(example)` and you would get the same result. – thelatemail May 29 '17 at 02:08
  • @user3148607 Could you be more specific? It working fine for me – h3rm4n May 29 '17 at 10:41