RoyalTS
RoyalTS

Reputation: 10203

flatten nested list at every level

I've got a nested list that looks like so:

> spec

$Data[[1]]
$Data[[1]]$Nusers
[1] 5292

$Data[[2]]
$Data[[2]]$Nitems
[1] 4890

$Data[[3]]
$Data[[3]]$Nsessions
[1] 45618

...

$Parameters[[4]]
$Parameters[[4]]$ICgroups
$Parameters[[4]]$ICgroups[[1]]
$Parameters[[4]]$ICgroups[[1]]$group_1
$Parameters[[4]]$ICgroups[[1]]$group_1[[1]]
$Parameters[[4]]$ICgroups[[1]]$group_1[[1]]$ICidx
[1] "1-1"

$Parameters[[4]]$ICgroups[[1]]$group_1[[2]]
$Parameters[[4]]$ICgroups[[1]]$group_1[[2]]$ICeffects
[1] "1-5"

$Parameters[[4]]$ICgroups[[1]]$group_1[[3]]
$Parameters[[4]]$ICgroups[[1]]$group_1[[3]]$ICeffectsPrice
[1] "1-3"

$Parameters[[4]]$ICgroups[[2]]
$Parameters[[4]]$ICgroups[[2]]$group_2
$Parameters[[4]]$ICgroups[[2]]$group_2[[1]]
$Parameters[[4]]$ICgroups[[2]]$group_2[[1]]$ICidx
[1] "2-173"

$Parameters[[4]]$ICgroups[[2]]$group_2[[2]]
$Parameters[[4]]$ICgroups[[2]]$group_2[[2]]$ICeffects
[1] "6-10"

$Parameters[[4]]$ICgroups[[2]]$group_2[[3]]
$Parameters[[4]]$ICgroups[[2]]$group_2[[3]]$ICeffectsPrice
[1] "4-6"

I would like to flatten this list at every level so that I could access e.g. spec$Parameters$ICgroups$group_1$ICidx but my purrr fu is failing me.

Upvotes: 1

Views: 165

Answers (1)

Maurits Evers
Maurits Evers

Reputation: 50668

What about using plyr::llply with unlist?

I'm using lst from my solution to your previous post.

require(plyr);
llply(lst, unlist)
#$Data
#           datadir             Nusers
#"/data/2017-11-22"             "5292"
#
#$Parameters
#                outdir                      K                     IC
#"/data/2017-11-22/out"                   "20"                  "179"
#      ICgroups.group 1     ICgroups.ICeffects       ICgroups.group 2
#                 "1-1"                  "1-5"                "2-173"
#    ICgroups.ICeffects       ICgroups.group 3     ICgroups.ICeffects
#                "6-10"              "175-179"                "11-15"

Upvotes: 1

Related Questions