Skip to content

Instantly share code, notes, and snippets.

@dickoa
Created December 30, 2021 20:10
Show Gist options
  • Save dickoa/391f0efb8a5e6b80aae09440b18437ae to your computer and use it in GitHub Desktop.
Save dickoa/391f0efb8a5e6b80aae09440b18437ae to your computer and use it in GitHub Desktop.
Have all repeat into a list of data.frame
#' @noRd
repeat_to_df <- function(x, form) {
nm <- unique(form$name[form$type %in% "begin_repeat"])
nm <- intersect(names(x), nm)
if (length(nm) > 0) {
x <- lapply(nm, function(n) {
res <- x[[n]]
res <- res[vapply(res, is.data.frame, logical(1))]
res <- rbindlist(res, fill = TRUE)
})
} else {
x <- NULL
}
x
}
#' @noRd
kobo_repeat_df <- function(x, form) {
if (is.null(x))
return(NULL)
x <- repeat_to_df(x, form)
lapply(x, function(x)
if (is.data.frame(x))
list(x, repeat_to_df(x, form))
else NULL)
}
@dickoa
Copy link
Author

dickoa commented Dec 30, 2021

library(robotoolbox)
asset <- kobo_asset("a4QDRGskBjNSwyXUS9vqPE")
form <- kobo_form(asset)
raw <- kobo_submissions(raw)
y <- kobo_repeat_df(raw, form)
y
[[1]]
[[1]][[1]]
     hh_lang  hh_name hh_age hh_sex n_pet
 1:    fr ar    Hakim     30   male     0
 2:       en    Alice     45 female     1
 3:    en fr      Ali     33   male     0
 4:       ar     Ibra     20   male     0
 5:    fr ar   Zeinab     36 female     2
 6:       en    Aicha     20 female     0
 7:    fr ar    Abdel     25   male     0
 8: en fr ar     Koro     50   male     2
 9:    fr ar     Kady     23 female     3
10:    en fr      Joe      9   male     1
11:       en   Albert     40   male     3
12:       en    Marie     30 female     0
13:       ar    Ahmed     38   male     2
14:       fr Michelle     18 female     1
15:       en   Audrey     20 female     2
16:       fr    Isaac     25   male     1
17:       en     Toto     20   male     2
18:       fr     Mame     35 female     1
19:       ar     Isma     15   male     4
    pet_characteristics_count pet_characteristics hh_lang_ar
 1:                         0   <data.frame[1x4]>          1
 2:                         1   <data.frame[1x4]>          0
 3:                         0                  NA          0
 4:                         0                  NA          1
 5:                         2   <data.frame[2x4]>          1
 6:                         0                  NA          0
 7:                         0                  NA          1
 8:                         2   <data.frame[2x4]>          1
 9:                         3   <data.frame[3x4]>          1
10:                         1   <data.frame[1x4]>          0
11:                         3   <data.frame[3x4]>         NA
12:                         0                  NA         NA
13:                         2   <data.frame[2x4]>          1
14:                         1   <data.frame[1x4]>          0
15:                         2   <data.frame[2x4]>          0
16:                         1   <data.frame[1x4]>          0
17:                         2   <data.frame[2x4]>          0
18:                         1   <data.frame[1x4]>          0
19:                         4   <data.frame[4x4]>          1
    hh_lang_en hh_lang_fr
 1:          0          1
 2:          1          0
 3:          1          1
 4:          0          0
 5:          0          1
 6:          1          0
 7:          0          1
 8:          1          1
 9:          0          1
10:          1          1
11:          1         NA
12:          1         NA
13:          0          0
14:          0          1
15:          1          0
16:          0          1
17:          1          0
18:          0          1
19:          0          0

[[1]][[2]]
[[1]][[2]][[1]]
    pet_type pet_name pet_age pet_sex
 1:      cat     Dodo      10    male
 2:   rabbit   Rabbit       5  female
 3:      cat    Choco       3    male
 4:      cat   Chacha       2  female
 5:      cat    Minou       5    male
 6:   rabbit     Noel       5    male
 7:      cat  Minette       1  female
 8:      cat    Ninou       5  female
 9:   rabbit  Carotte       1    male
10:   rabbit     Leuk       4    male
11:      dog     Dogo       3    male
12:      dog   Brutus       5    male
13:      dog    Titus       3  female
14:      dog  Derrick       3    male
15:   rabbit    Lapin       3    male
16:      cat     Titi       5  female
17:      dog  Biscuit       6    male
18:      cat    Belle       3  female
19:   rabbit  Lapinou       4    male
20:      cat     Mimi       3  female
21:      dog     Woof      10    male
22:   rabbit    Roger       2    male
23:      cat      Vik       2  female
24:      cat   Tigrou      10    male
25:      cat   Aristo       5    male
26:      cat     Lala       6  female
    pet_type pet_name pet_age pet_sex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment