在列表中合并两个df一对多 - R

时间:2018-06-13 14:21:35

标签: r

首先,我将忽略列表的使用,并使用两个df显示我想要的内容。

我有df1

ID    v1    Join_ID
1     100     1
2     110     2
3     150     3

df2

Join_ID    Type    v2
1          a       80
1          b       90
2          a       70
2          b       60
3          a       50
3          b       40

我希望df.join为:

ID     v1    Join_ID    a_v2    b_v2
1      100       1       80      90
2      110       2       70      60
3      150       3       50      40

我试过了:

df.merged <- merge(df1, df2, by="Join_ID")
df.wide <- dcast(melt(df.merged, id.vars=c("ID", "type")), ID~variable+type)

但是这会重复每种类型的df1中的所有变量:v1_a v1_b

除此之外,我还有两个列表

list.1
df1_a
df1_b
df1_c

list.2
df2_a
df2_b
df2_c

我希望列表1中的df1_a加入列表2中的df2_a

2 个答案:

答案 0 :(得分:1)

我们可以map通过list元素执行此操作,然后执行连接

library(tidyverse)
map2(list.1, list.2, ~ 
           .y %>%
              mutate(Type = paste0(Type, "_v2")) %>%
              spread(Type, v2) %>% 
              inner_join(.x, by = 'Join_ID'))

数据

df1 <- structure(list(ID = 1:3, v1 = c(100L, 110L, 150L), Join_ID = 1:3), 
   .Names = c("ID", 
"v1", "Join_ID"), class = "data.frame", row.names = c(NA, -3L
))

df2 <- structure(list(Join_ID = c(1L, 1L, 2L, 2L, 3L, 3L), Type = c("a", 
"b", "a", "b", "a", "b"), v2 = c(80L, 90L, 70L, 60L, 50L, 40L
)), .Names = c("Join_ID", "Type", "v2"), class = "data.frame", row.names = c(NA, 
-6L))

list.1 <- list(df1_a = df1, df1_b = df1, df1_c = df1)
list.2 <- list(df2_a = df2, df2_b = df2, df2_c = df2)

答案 1 :(得分:0)

对您的要求的一些回复:
     1.重塑df2
     2.具有不同列名的连接

库(reshape2)

    df1=data.frame(id=c(1,2,3), v1=c(100,110,150))
    df2=data.frame(Join_ID=c(1,1,2,2,3,3),Type=c("a","b","a","b","a","b"),v2=c(80,90,70,60,50,40))

    cast_df2=dcast(df2, Join_ID ~ Type)
    mergedData <- full_join(df1,cast_df2, by=c("id"="Join_ID"),suffixes=c("_df1","_df2") )