I have two lists comprising of several dataframes each, but in different order. List 1 has gene expression values and list 2 has gene membership in clusters. I would like to perform an inner join based on the item names, but how can I avoid doing it without having to specifically run each inner join separately?
lst1 <- list(Df1, Df2, Df3)
lst2 <- list(Df2, Df3, Df1)
Code I want to avoid:
gene_cluster[["Df1"]] <- lst1[["Df1"]] %>%
inner_join(lst2[["Df1"]] , by = "gene_name")
gene_cluster[["Df2"]] <- lst1[["Df2"]] %>%
inner_join(lst2[["Df2"]] , by = "gene_name")
gene_cluster[["Df3"]] <- lst1[["Df3"]] %>%
inner_join(lst2[["Df3"]] , by = "gene_name")
Thanks!
CodePudding user response:
The following should do what you want. We first name the elements of each list with the name of each dataframe and then, ordering both lists by name, we iterate with purrr::map2
, performing the inner joins.
library(purrr)
lst1 <- list(Df1, Df2, Df3)
lst2 <- list(Df2, Df3, Df1)
names(lst1) <- c("Df1", "Df2", "Df3")
names(lst2) <- c("Df2", "Df3", "Df1")
map2(lst1[order(names(lst1))], lst2[order(names(lst2))],
~ inner_join(.x, .y, by = "gene_name") )