Home:ALL Converter>Merge dataframes stored in two lists of the same length

Merge dataframes stored in two lists of the same length

Ask Time:2021-09-22T05:42:29         Author:Paul

Json Formatter

I have two long lists of large dataframes that are equal in length. I want to merge Dataframe1 (from list1) with Dataframe1 (from list2) and Dataframe2 (from list1) with Dataframe2 (from list2) etc...

Below is a minimal reproducible example and some attempts.

#Create Dataframes
df_1 <- data.frame(c("Bah",NA,2,3,4),c("Bug",NA,5,6,NA))
df_2 <- data.frame(c("Blu",7,8,9,10),c(NA,NA,NA,12,13))
df_3 <- data.frame(c("Bah",NA,21,32,43),c("Rgh",NA,51,63,NA))
df_4 <- data.frame(c("Gar",7,8,9,10),c("Ghh",NA,NA,121,131))

#Create Lists
list1 <- list(df_1,df_2)
list2 <- list(df_3,df_4)

#Set column and row names for each dataframe
colnames(list1[[1]]) <-  c("SampleID","Measure1","Measure2","Measure3","Measure4")
colnames(list1[[2]]) <-  c("SampleID","Measure1","Measure2","Measure3","Measure4")
colnames(list2[[1]]) <-  c("SampleID","Measure1","Measure2","Measure3","Measure4")
colnames(list2[[2]]) <-  c("SampleID","Measure1","Measure2","Measure3","Measure4")

rownames(list1[[1]]) <-  c("1","2")
rownames(list1[[2]]) <-  c("1","2")
rownames(list2[[1]]) <-  c("1","2")
rownames(list2[[2]]) <-  c("1","2")

My desired output is a list of the same length as the input lists but with each dataframe merged by position into a single dataframe. The following yields my desired output for the dataframes and list but is low throughput.

DesiredOutput_DF1_Format <- merge(list1[[1]],list2[[1]], all = TRUE, by = "SampleID")
DesiredOutput_DF2_Format <- merge(list1[[2]],list2[[2]], all = TRUE, by = "SampleID")
DesiredOutput_List <- list(DesiredOutput_DF1_Format, DesiredOutput_DF2_Format)

How can I generate an output list in my desired format in a highthroughput way using an apply-like approach?

attempt1 <- mapply(cbind, list1, list2, simplify=FALSE)

My instinct is to use `lapply` but i cant figure how to make it iterate through two lists simultaneously.

#Attempt3: Works but the order of the output list appears inverted. This is not intuitive, though it is easily corrected... There has to be a cleaner way.
output_list <- list()
dataset_iterator <- 1:length(list1)

for (x in dataset_iterator) {
    df1 <- data.frame(list1[[x]])
    df2 <- data.frame(list2[[x]])
    df_merged <- data.frame(merge(df1, df2, by = "Barcodes", all=TRUE))
    output_list <- append(output_list, list(df_merged), 0)

Author:Paul,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/69275863/merge-dataframes-stored-in-two-lists-of-the-same-length