简体   繁体   中英

Combine/ dataframes across lists

I am trying to merge like-named dataframes between two lists of dataframes using rbind or similar. The background is that a user is importing files containing data for 130 different compounds as a unique dataframes, so there's a list of 130 data frames within each data file imported. The data files are imported as a list, so there's a list of data files containing lists of dataframes.

The compounds/dataframe names remain constant day to day, but the number of files being imported is variable dependent on user preference.

I would like to rbind the dataframes together by compound. Here's a reproducible example that does what I want.

list.of.lists <- list(
        df.list1 <- list(df1 = data.frame("ID" = letters[1:10],
                                          "Data" = rnorm(10, 5, 2)),
                         df2 = data.frame("ID" = letters[1:10],
                                          "Data" = rnorm(10, 5, 2))
                         
        ),
        df.list2 <- list(df1 = data.frame("ID" = letters[11:20],
                                          "Data" = rnorm(10, 5, 2)),
                         df2 = data.frame("ID" = letters[11:20],
                                          "Data" = rnorm(10, 10, 2))
        )
)

j <- Map(rbind, list.of.lists[[1]], list.of.lists[[2]]) # Results in a list of data frames (df1, df2)

This generates a list of 2 data frames with 20 results in each, exactly what I want, but when applied to real-world, I don't know how many lists will be contained in the list.of.lists .

I suspect the answer is relatively straight forward, but I'm a little stuck. Help is much appreciated!

I suspect that you're looking for the function do.call :'

lapply(list.of.lists, function(x){
  do.call(what = rbind, x)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM