Hi all,
I am manipulating named lists with depth 1 and with elements being vectors of variable lengths.
e.g.
d <- list(a = 1:2, b = 3:7)
d
#> $a
#> [1] 1 2
#>
#> $b
#> [1] 3 4 5 6 7
I would like to turn these list into data frames for simplicity, such as:
#> name value
#> 1 a 1
#> 2 a 2
#> 3 b 3
#> 4 b 4
#> 5 b 5
#> 6 b 6
#> 7 b 7
I managed using base-r Map()
:
do.call("rbind", args = c(Map(\(x, y) data.frame(name = x, value = y), names(d), d), make.row.names = FALSE))
... but I am looking for a more elegant approach (tidy or not) that would be human readable and yet compact.
Any idea very welcome!
PS: I was hoping to discover some nice function hidden within {purrr}, but I probably missed it.