0

I have a list of 9 dataframes with the same columnames (called datalist) The dataframes (called L01, L02, ... L09) have all the following structure:

$L01
               time pressure    level abs_level
2021-04-22 00:00:00 21.05800 2.146454  12.33845
2021-04-22 00:15:00 21.09500 2.150225  12.34223
2021-04-22 00:30:00 21.04800 2.145435  12.33743
> str(datalist)
List of 10
 $ L01     :'data.frame':   9792 obs. of  4 variables:
  ..$ time     : POSIXct[1:9792], format: "2021-04-22 00:00:00" "2021-04-22 00:15:00" "2021-04-22 00:30:00" "2021-04-22 00:45:00" ...
  ..$ pressure : num [1:9792] 21.1 21.1 21 21 21 ...
  ..$ level    : num [1:9792] 2.15 2.15 2.15 2.14 2.14 ...
  ..$ abs_level: num [1:9792] 12.3 12.3 12.3 12.3 12.3 ...
 $ L02     :'data.frame':   9792 obs. of  4 variables:
  ..$ time     : POSIXct[1:9792], format: "2021-04-22 00:00:00" "2021-04-22 00:15:00" "2021-04-22 00:30:00" "2021-04-22 00:45:00" ...
  ..$ pressure : num [1:9792] 19.4 19.4 19.3 19.3 19.3 ...
  ..$ level    : num [1:9792] 1.97 1.98 1.97 1.97 1.97 ...
  ..$ abs_level: num [1:9792] 12.4 12.4 12.4 12.4 12.4 ...

...

I have a similar addlist with the same 9 dataframes (also L01, L02, ... L09) of the same structure (same columns and same order), but these data are from a different time period. But I just want to merge the dataframes of the addlist to the datalist, so that the rows of dataframe L01 of addlist are added to the dataframe of L01 in the datalist), L02 to L02, and so on for the rest of the dataframes.

So actually very easy... I thought. I can't find the answer clearly on the internet. I was thinking of using rbind, but I don't know how to use that in the context of the two lists.

How can I do this? Any suggestions? Many thanks in advance!

0

4 Answers 4

2

Does this work?

finallist <- list()

for(i in 1:length(datalist)){
    finallist[[i]] <- rbind(datalist[[i]],addlist[[i]])
}

There's probably a more efficient way to do this, but this should work if the lists you want to combine are in the same order and have the same columns

Sign up to request clarification or add additional context in comments.

4 Comments

You aren't assigning the result of rbind to anything here; surely you'd need to initialize a "results" list and then store the bound dataframes in that?
Yep--just edited it and checked it with two objects in my current R environment to make sure it works this time
Are you sure this works? I think this will overwrite finallist in every iteration. Lastly, finallist will have output for only last combination.
Correct again, thanks--just updated to fix that (I used a list of 1 to test)
2

You can try the base R code below

Map(rbind, datalist, addlist)

Comments

1

you can use

df <- do.call('rbind', your_list)

or

library(data.table)

df <- rbindlist(your_list)

Comments

0

Using purrr from the tidyverse, you could try:

library(purrr)
map2(datalist, addlist, ~ rbind(.x, .y))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.