R - How do I column bind all vectors from a list of vectors into a separate object?

Solution 1:

A possible solution:

library(tidyverse)

mylst <- c(str_c("knot", 1:4) %>% map(~ get(.x)), list_knot)

bind_cols(mylst %>% set_names(c(str_c("knot",1:4),
          str_c("v", 1:length(list_knot)))))

#> # A tibble: 800 × 9
#>    knot1 knot2   knot3      knot4    v1    v2    v3    v4    v5
#>    <dbl> <dbl>   <dbl>      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1     1 -400  160000  -64000000      0    -7   -26   -63  -124
#>  2     1 -399. 159200. -63520601.     1    -6   -25   -62  -123
#>  3     1 -398. 158402. -63043602.     2    -5   -24   -61  -122
#>  4     1 -397. 157606. -62568998.     3    -4   -23   -60  -121
#>  5     1 -396. 156812. -62096781.     4    -3   -22   -59  -120
#>  6     1 -395. 156020. -61626946.     5    -2   -21   -58  -119
#>  7     1 -394. 155230. -61159487.     6    -1   -20   -57  -118
#>  8     1 -393. 154442. -60694398.     7     0   -19   -56  -117
#>  9     1 -392. 153656. -60231672.     8     1   -18   -55  -116
#> 10     1 -391. 152872. -59771305.     9     2   -17   -54  -115
#> # … with 790 more rows

Or:

library(tidyverse)

map_dfc(str_c("knot", 1:4), ~ list(get(.x)) %>% set_names(.x)) %>% 
  bind_cols(list_knot %>% set_names(str_c("v", 1:length(list_knot))))

#> # A tibble: 800 × 9
#>    knot1 knot2   knot3      knot4    v1    v2    v3    v4    v5
#>    <dbl> <dbl>   <dbl>      <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1     1 -400  160000  -64000000      0    -7   -26   -63  -124
#>  2     1 -399. 159200. -63520601.     1    -6   -25   -62  -123
#>  3     1 -398. 158402. -63043602.     2    -5   -24   -61  -122
#>  4     1 -397. 157606. -62568998.     3    -4   -23   -60  -121
#>  5     1 -396. 156812. -62096781.     4    -3   -22   -59  -120
#>  6     1 -395. 156020. -61626946.     5    -2   -21   -58  -119
#>  7     1 -394. 155230. -61159487.     6    -1   -20   -57  -118
#>  8     1 -393. 154442. -60694398.     7     0   -19   -56  -117
#>  9     1 -392. 153656. -60231672.     8     1   -18   -55  -116
#> 10     1 -391. 152872. -59771305.     9     2   -17   -54  -115
#> # … with 790 more rows

Solution 2:

A base R approach.

names(list_knot) <- paste0('knot_list', seq_len(list_length))
list2env(list_knot, globalenv())
df <- data.frame(mget(grep('knot[0-9]+', ls(), value = TRUE), globalenv()),
                 mget(grep('_list[0-9]+', ls(), value = TRUE), globalenv()))

Result

> head(df[, -c(1:3)], 4)
      knot4 knot_list1 knot_list2 knot_list3 knot_list4 knot_list5
1 -64000000          0         -7        -26        -63       -124
2 -63520601          1         -6        -25        -62       -123
3 -63043602          2         -5        -24        -61       -122
4 -62568998          3         -4        -23        -60       -121