Home > Software engineering >  R: combine vector elements with same names in list into list of dataframes
R: combine vector elements with same names in list into list of dataframes

Time:05-30

I have a list of about 100,000 vectors, all with the same length of about 1,400 numbers. Some vectors have the same names (always by group of 5). I need to convert this list of vectors into a list of data frames combining the vectors having the same names by column. For now I use:

my_list %<>% split(names(.)) %>% map(bind_cols)

which works great but is very slow. It takes about 1s to process two groups so almost 3 hours in total which seems very slow for such a task. Would there be any other faster method to try for this?

Here is an example:

my_list <- list(
  v1 = c(1,2,3,4,5,6,7,8,9),
  v1 = c(1,2,3,4,5,6,7,8,9),
  v1 = c(1,2,3,4,5,6,7,8,9),
  v2 = c(1,2,3,4,5,6,7,8,9),
  v2 = c(1,2,3,4,5,6,7,8,9),
  v2 = c(1,2,3,4,5,6,7,8,9),
  v3 = c(1,2,3,4,5,6,7,8,9),
  v3 = c(1,2,3,4,5,6,7,8,9),
  v3 = c(1,2,3,4,5,6,7,8,9)
)
my_list %>% split(names(.)) %>% map(bind_cols)

CodePudding user response:

Perhaps, we can cbind all of them at once to a single dataset and then split

split.default(as.data.frame(my_list), names(my_list))
  •  Tags:  
  • r
  • Related