合并具有多列的数据框列表,只有唯一列名称为date_time,某些列名称相同,有些列不同

时间:2016-06-25 16:43:40

标签: r dataframe

dat1 <- data.frame(Date_Time = c("20120628", "20120629 1045", "20120723 1100", "20120726 1500"),
                          O = c(2345, 4567, 6780, 3456),
                         '2000C' = c(NA, 250, 100, NA),
                          PUT.1 = c(4, NA, 50, 50),
                         '3000C' = c(NA, 45, 50, 70),
                          PUT.2 = c(50, 40, NA, 20))

dat2 <- data.frame(Date_Time = c("20120728 1200", "20120829 1145", "20120923 1100", "201201026 1400"),
                   O = c(4345, 4567, 5080, 4456),
                   '4000C' = c(NA, 250, 100, NA),
                   PUT.4 = c(4, NA, 50, 50),
                   '5000C' = c(NA, 45, 50, 70),
                   PUT.5 = c(50, 40, NA, 20))

这些是可重复的例子,我有一个如上所述的数据框列表,我想将它们全部合并,但每个都有多个列名,它们在所有数据框中都在不断变化。帮助

我已经习惯了 res <- merge(dt1, dt2, by = ("Date_Time"), all.x = TRUE)虽然只是为了查看是否只合并了两个dt,但它只显示Date_Time之一的dt列值,而不显示其他date_time列值,我没有看到其他df被合并的foos_bars列。请帮忙解决这个问题很长一段时间。

1 个答案:

答案 0 :(得分:0)

dt1 <- data.table(Date_Time = c("20120628", "20120629 1045", "20120723 1100", "20120726 1500"),
                  O = c(2345, 4567, 6780, 3456),
                  '2000C' = c(NA, 250, 100, NA),
                  PUT.1 = c(4, NA, 50, 50),
                  '3000C' = c(NA, 45, 50, 70),
                  PUT.2 = c(50, 40, NA, 20))

dt2 <- data.table(Date_Time = c("20120728 1200", "20120829 1145", "20120923 1100", "201201026 1400"),
                  O = c(4345, 4567, 5080, 4456),
                  '4000C' = c(NA, 250, 100, NA),
                  PUT.4 = c(4, NA, 50, 50),
                  '5000C' = c(NA, 45, 50, 70),
                  PUT.5 = c(50, 40, NA, 20))

res <- merge(dt1, dt2, by = ("Date_Time"), all.x = TRUE)