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

Time:01-24

This is the set up, and we have a list of 5 vectors, with 800 numbers in each.

h5=rep(0,800);h6=h5;h7=h5;h8=h5;h9=h5
list_knot<- list(h5,h6,h7,h8,h9)
list_length <- length(list_knot) # 5 
x=seq(from=-400,to=400,length.out=800)
knot1 = rep(1,length(x))
knot2 = x
knot3 = x^2
knot4 = x^3

vec1 <- list(c(1, 2, 3, 4, 5)) # as per your requirement
list_knot <- lapply(vec1[[1]], function(v, x) x - v^3, x = 1:800)

cbind(knot1,knot2,knot3,knot4,list_knot[[1]],list_knot[[2]],.... etc)

I was wondering how I could take each vector and create a new object via cbind() without manually calling each vector.

CodePudding user response:

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

CodePudding user response:

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
  •  Tags:  
  • Related