Home > database >  In R, how to construct vectors of the corresponding elements of the data frames
In R, how to construct vectors of the corresponding elements of the data frames

Time:03-02

I have a list of data frames, for example the first three

[[1]]
01oct 24sep 17sep 10sep 03sep 27aug 20aug 13aug 06aug 30jul 23jul 16jul 09jul 02jul 25jun 18jun 11jun 04jun 28may 21may 14may 07may 30apr 23apr 
 3.25  9.50  0.80  6.85  6.70  6.65 14.35 62.35  9.75  2.35 18.55  8.90 17.85 14.75  0.90  0.50 17.05 19.15 44.25  0.15 42.05 10.45 12.00  5.05 
16apr 09apr 02apr 
 0.15 12.90 23.20 

[[2]]
30sep 23sep 16sep 09sep 02sep 26aug 19aug 12aug 05aug 29jul 22jul 15jul 08jul 01jul 24jun 17jun 10jun 03jun 27may 20may 13may 06may 29apr 22apr 
 1.90  4.60 23.95  3.95 12.65 26.30 38.30  2.80  2.35 34.10  8.30  7.30 28.85  2.45  5.20 15.35  1.85 36.75  0.95  8.40 22.35 37.70  6.00  0.40 
15apr 08apr 
 3.25  5.45 

[[3]]
28sep 21sep 14sep 07sep 31aug 24aug 17aug 10aug 03aug 27jul 20jul 13jul 06jul 29jun 22jun 15jun 08jun 01jun 25may 18may 11may 04may 27apr 20apr 
 5.85 13.70  2.85 12.50 43.40 13.25  5.65  4.80 12.20  5.40  3.05 12.90 20.70 21.75 13.20 18.60  0.70 13.15 20.30  2.40  2.30 13.50  4.70 19.60 
13apr 06apr 
17.60 14.50 

I am trying to create vectors of the corresponding elements of each data frame. In the above example, the first three elements of my first vector would be 3.25, 1.90, 5.85. The second vector would be 9.5, 4.6, 13.7. The strings showing dates ideally would be left out, since at a later stage the vectors will be used to compute correlations. My ultimate goal is an array of these vectors.

I know this could be done with a nested loops, however I've tried and have other problems with this kind of array assignment in R (but that's for another thread). I also know that nested loops are inefficient and not best practice (at least I understood that).

What is the most reproducible way to construct these vectors and the array of them in R?

CodePudding user response:

library(tidyverse)

# example data
l <- list(
  data.frame(a = 3.25, b = 9.5),
  data.frame(c = 1.90, d = 4.6),
  data.frame(e = 5.85, d = 13.70)
)

combined <-
  l %>%
  map(~ {
    # unify column names
    colnames(.x) <- colnames(.x) %>% length() %>% seq()
    .x
  }) %>%
  reduce(bind_rows)

combined[[1]]
#> [1] 3.25 1.90 5.85
combined[[2]]
#> [1]  9.5  4.6 13.7

Created on 2022-03-01 by the reprex package (v2.0.0)

CodePudding user response:

It sounds like you want a transposition of the vectors. This is going to run into problems, because to do so suggests that the vectors should all be the same length, which they're not.

lengths(yourlist)
# [1] 27 26 26

We can fix that by padding them with NA,

yourlist <- lapply(yourlist, `length<-`, max(lengths(yourlist)))

yourlist
# [[1]]
# 01oct 24sep 17sep 10sep 03sep 27aug 20aug 13aug 06aug 30jul 23jul 16jul 09jul 02jul 25jun 18jun 11jun 04jun 28may 21may 14may 07may 30apr 23apr 16apr 09apr 02apr 
#  3.25  9.50  0.80  6.85  6.70  6.65 14.35 62.35  9.75  2.35 18.55  8.90 17.85 14.75  0.90  0.50 17.05 19.15 44.25  0.15 42.05 10.45 12.00  5.05  0.15 12.90 23.20 
# [[2]]
# 30sep 23sep 16sep 09sep 02sep 26aug 19aug 12aug 05aug 29jul 22jul 15jul 08jul 01jul 24jun 17jun 10jun 03jun 27may 20may 13may 06may 29apr 22apr 15apr 08apr       
#  1.90  4.60 23.95  3.95 12.65 26.30 38.30  2.80  2.35 34.10  8.30  7.30 28.85  2.45  5.20 15.35  1.85 36.75  0.95  8.40 22.35 37.70  6.00  0.40  3.25  5.45    NA 
# [[3]]
# 28sep 21sep 14sep 07sep 31aug 24aug 17aug 10aug 03aug 27jul 20jul 13jul 06jul 29jun 22jun 15jun 08jun 01jun 25may 18may 11may 04may 27apr 20apr 13apr 06apr       
#  5.85 13.70  2.85 12.50 43.40 13.25  5.65  4.80 12.20  5.40  3.05 12.90 20.70 21.75 13.20 18.60  0.70 13.15 20.30  2.40  2.30 13.50  4.70 19.60 17.60 14.50    NA 

Given that, a list-transpose:

yourlist2 <- do.call(Map, c(list(f = c, use.names = FALSE), yourlist))
str(yourlist2)
# List of 27
#  $ : num [1:3] 3.25 1.9 5.85
#  $ : num [1:3] 9.5 4.6 13.7
#  $ : num [1:3] 0.8 23.95 2.85
#  $ : num [1:3] 6.85 3.95 12.5
#  $ : num [1:3] 6.7 12.6 43.4
#  $ : num [1:3] 6.65 26.3 13.25
#  $ : num [1:3] 14.35 38.3 5.65
#  $ : num [1:3] 62.4 2.8 4.8
#  $ : num [1:3] 9.75 2.35 12.2
#  $ : num [1:3] 2.35 34.1 5.4
#  $ : num [1:3] 18.55 8.3 3.05
#  $ : num [1:3] 8.9 7.3 12.9
#  $ : num [1:3] 17.9 28.9 20.7
#  $ : num [1:3] 14.75 2.45 21.75
#  $ : num [1:3] 0.9 5.2 13.2
#  $ : num [1:3] 0.5 15.3 18.6
#  $ : num [1:3] 17.05 1.85 0.7
#  $ : num [1:3] 19.1 36.8 13.2
#  $ : num [1:3] 44.25 0.95 20.3
#  $ : num [1:3] 0.15 8.4 2.4
#  $ : num [1:3] 42 22.4 2.3
#  $ : num [1:3] 10.4 37.7 13.5
#  $ : num [1:3] 12 6 4.7
#  $ : num [1:3] 5.05 0.4 19.6
#  $ : num [1:3] 0.15 3.25 17.6
#  $ : num [1:3] 12.9 5.45 14.5
#  $ : num [1:3] 23.2 NA NA
  • Related