R-基于多个列匹配合并多个数据帧

时间:2019-04-28 16:56:01

标签: r dataframe merge multiple-columns matching

我的目标是根据两列中的匹配项,有选择地将几个数据帧有选择地组合成一个单独的步。

主要数据集

structure(list(OB_END_TIME = c("2006-01-01 01:00", "2006-01-01 00:00", 
"2006-01-01 00:00", "2006-01-01 01:00", "2006-01-01 01:00", "2006-01-01 18:00", 
"2006-01-01 01:00", "2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00"
), Weather.Category = c("Wind", "Flood", "Flood", "Wind", "Wind", 
"Wind", "Wind", "Wind", "Adhesion", "Subsidence"), SRC_ID = c(52L, 
67L, 103L, 2719L, 105L, 32L, 113L, 6704L, 117L, 
9569L), distance = c(4337.916253, 2825.720696, 2825.720696, 2397.887245, 
4513.448694, 3787.205117, 3774.967612, 4925.782405, 1178.442845, 
1748.086262), PRCP_AMT = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA), MEAN_WIND_DIR = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), MEAN_WIND_SPEED = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
), MAX_GUST_DIR = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
    MAX_GUST_SPEED = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    )), row.names = c(1L, 2L, 3L, 4L, 5L, 10L, 11L, 12L, 13L, 
14L), class = "data.frame")

支持数据集1

structure(list(OB_END_TIME = c("2006-01-01 00:00", "2006-01-01 00:00", 
"2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00", 
"2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00"
), SRC_ID = c(32L, 44L, 52L, 67L, 103L, 79L, 105L, 
117L, 113L, 30L), PRCP_AMT = c(0, 0, 0, 0.6, 0, 0, 0.4, 0.2, 
0.2, 0.2), HIGH_PRCN_LAT = c(1, 2, 44, 65, 
1, 1, 23, 43, 54.1346, 54.04502)), row.names = c(NA, 10L), class = "data.frame")

支持数据集2

structure(list(OB_END_TIME = c("2006-01-01 00:00", "2006-01-01 00:00", 
"2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00", 
"2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00"
),  SRC_ID = c(105L, 44L, 52L, 30L, 67L, 79L, 103L, 32L, 114L, 
    117L), MEAN_WIND_DIR = c(250L, 0L, 0L, 10L, 290L, 290L, 30L, 
    0L, 310L, 50L), MEAN_WIND_SPEED = c(3L, 0L, 0L, 10L, 5L, 
    8L, 17L, 0L, 1L, 24L), MAX_GUST_DIR = c(270L, 0L, 0L, 30L, 
    290L, 290L, 30L, 0L, 320L, 40L), MAX_GUST_SPEED = c(5L, 0L, 
    0L, 18L, 11L, 11L, 23L, 0L, 2L, 29L), HIGH_PRCN_LAT = c(1, 2, 44, 65, 
1, 1, 23, 43, 54.1346, 54.04502)), row.names = c(6L, 7L, 8L, 
10L, 11L, 12L, 16L, 17L, 18L, 19L), class = "data.frame")

r元数据

platform        x86_64-w64-mingw32
version.string  R version 3.5.3 (2019-03-11)

专有代码

dfX$OB_END_TIME <- format(as.POSIXct(strptime(rain$OB_END_TIME, "%Y-%m-%d %H:%M")), "%Y-%m-%d %H:%M")

所有数据框均已在其datatime列上运行了上述代码,以使其标准化以进行比较/匹配。

匹配代码

primaryDataFrame <- merge(x = primaryDataFrame, y = supportingDataFrame1, by = c("SRC_ID", "OB_END_TIME"))

预期结果(手动生成)

structure(list(OB_END_TIME = c("2006-01-01 01:00", "2006-01-01 00:00", 
"2006-01-01 00:00", "2006-01-01 01:00", "2006-01-01 00:00", "2006-01-01 18:00", 
"2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00"
), Weather.Category = c("Wind", "Flood", "Flood", "Wind", "Wind", 
"Wind", "Wind", "Wind", "Adhesion", "Subsidence"), SRC_ID = c(67L, 
52L, 103L, 2719L, 105L, 32L, 113L, 6704L, 117L, 9569L), distance = c(4337.916253, 2825.720696, 2825.720696, 2397.887245, 4513.448694, 3787.205117, 3774.967612, 4925.782405, 1178.442845, 1748.086262), PRCP_AMT = c(NA, 0, 0, NA, 0.4, NA, 0.2, NA, 0.2, NA), MEAN_WIND_DIR = c(NA, 0L, 30L, NA, 250L, NA, NA, NA, 50L, NA), MEAN_WIND_SPEED = c(NA, 0L, 17L, NA, 3L, NA, NA, NA, 24L, NA), MAX_GUST_DIR = c(NA, 0L, 30L, NA, 270L, NA, NA, NA, 40L, NA), 
    MAX_GUST_SPEED = c(NA, 0L, 23L, NA, 5L, NA, NA, NA, 29L, NA
    )), row.names = c(1L, 2L, 3L, 4L, 5L, 10L, 11L, 12L, 13L, 
14L), class = "data.frame")

实际结果(手动生成)

structure(list(OB_END_TIME = c("2006-01-01 00:00", "2006-01-01 00:00", 
"2006-01-01 00:00", "2006-01-01 00:00", "2006-01-01 00:00"), 
    Weather.Category = c("Flood", "Flood", "Wind", "Wind", "Adhesion"
    ), SRC_ID = c(52L, 103L, 105L, 113L, 117L), distance = c(2825.720696, 
    2825.720696, 4513.448694, 3774.967612, 1178.442845), PRCP_AMT.x = c(NA, NA, NA, NA, NA), PRCP_AMT.y = c(0, 
    0, 0.4, 0.2, 0.2), MEAN_WIND_DIR.x = c(NA, NA, NA, NA, NA), MEAN_WIND_DIR.y = c(0L, 30L, 250L, NA, 50L
    ), MEAN_WIND_SPEED.x = c(NA, NA, NA, NA, NA), MEAN_WIND_SPEED.y = c(0L, 17L, 3L, NA, 24L), MAX_GUST_DIR.x = c(NA, NA, NA, NA, NA, MAX_GUST_DIR.y = c(0L, 
    30L, 270L, NA, 40L), MAX_GUST_SPEED.x = c(NA, NA, NA, NA, NA), MAX_GUST_SPEED.y = c(0L, 23L, 5L, NA, 
    29L)), row.names = c(2L, 3L, 5L, 11L, 13L), class = "data.frame")

结果问题:

1)删除主数据集中不符合条件的列。

2)应将应填充数据的列复制到PRCP_AMT.xPRCP_AMT.y

3)在比赛(HIGH_PRCN_LAT)期间,将不需要的列添加到主数据集中。

1 个答案:

答案 0 :(得分:1)

对于问题1),您只需添加all.x = T即可:

primaryDataFrame <- merge(x = primaryDataFrame, y = supportingDataFrame1, by = c("SRC_ID", "OB_END_TIME"), all.x = T)

对于问题2),merge发生了。您有重复的列,因为它们的名称相同-您应为它们指定不同的名称,然后选择/重命名您希望保留的列。

您可以在3)中添加一列:

primaryDataFrame <- merge(x = primaryDataFrame, y = supportingDataFrame1, by = c("SRC_ID", "OB_END_TIME"), all.x = T) %>% 
    select(-HIGH_PRCN_LAT)