Joining dataframes from lists of unequal length

白昼怎懂夜的黑 提交于 2021-02-10 06:15:19

问题


I have two lists of dataframes that I want to combine, I have tried the following:

A <- data.frame(ID = c(1,2,3),
                var1 = c(3,4,5),
                var2 = c(6,3,2))
B <- data.frame(ID = c(1,2,3),
                var1 = c(4,4,5),
                var2 = c(6,7,3))
C <- data.frame(ID = c(1,2,3),
                var1 = c(1,4,8),
                var2 = c(9,2,3))

list1 <- list(A = A, B = B, C = C)
list2 <- list(A = A, B = B)

combined <- map2(list1, list2, full_join, by = 'ID')

This returns an error that the two lists are of different lengths. The only other way I have thought of is to add a blank dataframe to the second list so they are the same length.

Is it possible combine the two lists so that I get a single list where A1 has been joined with A2, B1 with B2, and C1 remains as it is?

Edit: Its been highlighted that I havent named the elements of the list, I have named them now


回答1:


If we have named lists, then we could:

list1 <- list(A = A, B = B, C = C)
list2 <- list(A = A, B = B)

x12 <- intersect(names(list1), names(list2))
x1 <- setdiff(names(list1), names(list2))
x2 <- setdiff(names(list2), names(list1))

combined <- c(
  map2(list1[ x12 ], list2[ x12 ], full_join, by = 'ID'),
  list1[ x1 ],
  list2[ x2 ])



回答2:


If you want to keep working with positions, you can also pass placeholders and handle them in the combining logic. Something like that:

skip <- tibble(ID = integer(0))

list1 <- list(A, B, C)
list2 <- list(A, B, skip)
list3 <- list(A, skip, C)

combined  <- map2(list1, list2, full_join, by = 'ID')
combined2 <- map2(list1, list3, full_join, by = 'ID')

Note how list2 and list3 would look exactly the same if not for the placeholders!

More generically:


list1 <- list(A, B, C)
list2 <- list(A, B, NULL)
list3 <- list(A, NULL, C)

combine <- function(list1, list2) {
   purrr::map2(list1, list2, function(df1, df2) {
      if (is.null(df1)) return(df2)
      if (is.null(df2)) return(df1)
      full_join(df1, df2, by = 'ID')
   })
}

combined  <- combine(list1, list2)
combined2 <- combine(list1, list3)



来源:https://stackoverflow.com/questions/58934147/joining-dataframes-from-lists-of-unequal-length

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!