Home > Software engineering >  bind columns of dfs in list to dfs stored in list of lists
bind columns of dfs in list to dfs stored in list of lists

Time:02-03

df1

I have a list of list of DFs

list_of_list <- list(list(df1, df2, df3, df1, df4),
list(df1, df2, df3, df4, df1),
list(df2, df3, df4))

and I have a list with corresponding dataframes

list2 <- list(df1, df2, df3)

Now I want to bind the columns of list2 to the dfs in the list of lists, when they share the same name (when they share the same name they also have the same amount on rows).

I tried something like:

test <- lapply(1:3, function(z) 
                        {Map(
                            function(x, y) {bind_cols(x,y)}, list_of_list[z][names(list2)], Countries[names(list_of_list[[z]])]);z })

But somehow that will not work and gives me a list of 1:3 with no elements. Someone have an idea? Thanks.

df1 <- data.frame( A = runif(n=10, min=1, max=20),  
                    B = runif(n=10, min=1, max=20))
df2<- data.frame( A = runif(n=10, min=1, max=20),  
                  B = runif(n=10, min=1, max=20))
df3 <- data.frame( A = runif(n=10, min=1, max=20),  
                   B = runif(n=10, min=1, max=20))


list1 <- list(df1, df2, df3)

names(list1)<- c("df1", "df2", "df3")

list_of_list<- list(list1, list1)

df4<- data.frame( C = runif(n=10, min=1, max=20),  
                  D = runif(n=10, min=1, max=20))
df5 <- data.frame( C = runif(n=10, min=1, max=20),  
                   D = runif(n=10, min=1, max=20))
df6 <- data.frame( C = runif(n=10, min=1, max=20),  
                   D = runif(n=10, min=1, max=20))

list2 <- list(df4, df5, df6)
names(list2) <- c("df1", "df2", "df3")

So C and D of df1 in list2 shall go to the df1s in my list_of_lists

CodePudding user response:

I'm sure you could do this with lapply and the like, but in a complex nested case like this where the names are important I think it's easier to use loops. (Note: I replaced all the n=10 in your sample data with n=2 so it's easier to see what's going on.)

for(i in seq_along(list_of_list)) {
  for(nm in names(list_of_list[[i]])) {
    if(nm %in% names(list2)) {
      list_of_list[[i]][[nm]] = bind_cols(list_of_list[[i]][[nm]], list2[[nm]])
    }
  }
  
}
list_of_list
# [[1]]
# [[1]]$df1
#           A        B        C         D
# 1 16.710355 13.97386 5.041697  8.700987
# 2  2.125478 16.06537 1.715032 14.783556
# 
# [[1]]$df2
#          A        B        C        D
# 1 11.01342 15.26083 19.15215 16.15012
# 2 17.79299 13.25432 14.89834 19.37026
# 
# [[1]]$df3
#           A        B        C        D
# 1 12.261895 19.68988 16.69863 19.87182
# 2  7.217829 19.86504 14.13768 18.88725
# 
# 
# [[2]]
# [[2]]$df1
#           A        B        C         D
# 1 16.710355 13.97386 5.041697  8.700987
# 2  2.125478 16.06537 1.715032 14.783556
# 
# [[2]]$df2
#          A        B        C        D
# 1 11.01342 15.26083 19.15215 16.15012
# 2 17.79299 13.25432 14.89834 19.37026
# 
# [[2]]$df3
#           A        B        C        D
# 1 12.261895 19.68988 16.69863 19.87182
# 2  7.217829 19.86504 14.13768 18.88725
  • Related