d <- list(a = 1:2, b = 3:7)
f <- data.frame(name = as.character(), value = as.integer())
shimmy <- function(x) {
a = expand.grid(d[x])
colnames(a) = "value"
a$names = names(expand.grid(d[x]))
return(a[2:1])
}
rbind(f,shimmy(1),shimmy(2))
#> names value
#> 1 a 1
#> 2 a 2
#> 3 b 3
#> 4 b 4
#> 5 b 5
#> 6 b 6
#> 7 b 7
I typically use tibble::enframe for this, and then unnest using tidyr::unnest:
library(tidyr)
library(tibble)
d <- list(a = 1:2, b = 3:7)
tibble::enframe(d) %>%
tidyr::unnest(value)
#> # A tibble: 7 × 2
#> name value
#> <chr> <int>
#> 1 a 1
#> 2 a 2
#> 3 b 3
#> 4 b 4
#> 5 b 5
#> 6 b 6
#> 7 b 7