首页 > 解决方案 > 带有两列的模糊连接,只有一对需要模糊连接语法的列

问题描述

我正在尝试 inner_join 两个数据框,每个数据框有三列。第一个数据框包含日期、变量名称和预测值,而第二个数据框包含日期、变量名称和实际值。我加入的目的是按日期和正确的天气变量名称将预测值与实际值相匹配,以进行准确度分析。

我的第一个数据框 x 如下

 x <- structure(list(Date = structure(c(1588060800, 1588060800, 1588060800, 
1588060800, 1588060800, 1588060800, 1588060800, 1588060800, 1588060800, 
1588060800, 1588060800, 1588060800), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), wx_vars = c("Wx1_Temperature", "Wx1_Precipitation", 
"Wx1_CloudCover", "Wx1_DewPoint", "Wx1_WindSpeed", "Wx1_SolarRadiation", 
"Wx2_Temperature", "Wx2_Precipitation", "Wx2_CloudCover", "Wx2_DewPoint", 
"Wx2_WindSpeed", "Wx2_SolarRadiation"), wx_forecast = c(56.92, 
0.0046, 77.46, 50.26, 7.42, 12.93, 57.05, 0.0037, 68.3, 50.5, 
7.32, 19.02)), row.names = c(NA, 12L), class = "data.frame")

数据框 x

我的第二帧 y 如下:

y <- structure(list(Date = structure(c(1588057200, 1588057200, 1588057200, 
1588057200, 1588060800, 1588060800, 1588060800, 1588060800, 1588060800, 
1588060800, 1588064400, 1588064400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), wx_vars = c("Actual_CloudCover", "Actual_WindSpeed", 
"Actual_Precipitation", "Actual_SolarRadiation", "Actual_Temperature", 
"Actual_DewPoint", "Actual_CloudCover", "Actual_WindSpeed", "Actual_Precipitation", 
"Actual_SolarRadiation", "Actual_Temperature", "Actual_DewPoint"
), wx_actuals = c(54.8, 5.63, 0, 26.1, 57.32, 49.99, 61, 7.24, 
0.00015, 23.4, 59.84, 52.11)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L))

数据框 y

如您所见,x 预测日期框架有两个独立的天气预报,我想评估它们的准确性:Wx1 和 Wx2。每行在 Wx1 或 Wx2 之后列出了一个不同的天气变量,它们与数据框 y 中的实际数据完全一致。日期在 x 和 y 中的格式相同,并且可以在简单的 inner_join 中工作,但考虑到数据框 x 和 y 的 wx_vars 列中的字符串差异,我一直在尝试使用模糊连接。还没有运气。

这是我尝试过的,我最大的问题是我作为问题标题包含的内容。我还没有找到一个在两列上进行模糊连接的示例,其中一个列匹配可以在常规连接中工作,而另一列匹配需要模糊连接,在我的情况下是部分字符串匹配。

wx_analysis_1<- fuzzy_inner_join(x, y, by = c("Date", "wx_vars"="wx_vars"), match_fun = str_detect)
wx_analysis_2 <- regex_inner_join(x, y, by = c("Date", "wx_vars"="wx_vars"))

这两种解决方案都不走运。我是否缺少一些语法来更好地处理日期加入?我觉得 wx_vars 的fuzzy_inner_join 应该可以工作,但日期列可能是我的问题。

谢谢

标签: rdplyrfuzzyjoin

解决方案


由于您知道列应该匹配的规则,因此更容易显式使用这些规则:

x <- x %>% 
  separate(wx_vars, c("type_wx", "wx_vars"), "_")

y <- y %>% 
  separate(wx_vars, c("type", "wx_vars"), "_")


x %>% 
  inner_join(y, by = c("Date", "wx_vars"))

#                   Date type_wx        wx_vars wx_forecast   type wx_actuals
# 1  2020-04-28 08:00:00     Wx1    Temperature     56.9200 Actual   57.32000
# 2  2020-04-28 08:00:00     Wx1  Precipitation      0.0046 Actual    0.00015
# 3  2020-04-28 08:00:00     Wx1     CloudCover     77.4600 Actual   61.00000
# 4  2020-04-28 08:00:00     Wx1       DewPoint     50.2600 Actual   49.99000
# 5  2020-04-28 08:00:00     Wx1      WindSpeed      7.4200 Actual    7.24000
# 6  2020-04-28 08:00:00     Wx1 SolarRadiation     12.9300 Actual   23.40000
# 7  2020-04-28 08:00:00     Wx2    Temperature     57.0500 Actual   57.32000
# 8  2020-04-28 08:00:00     Wx2  Precipitation      0.0037 Actual    0.00015
# 9  2020-04-28 08:00:00     Wx2     CloudCover     68.3000 Actual   61.00000
# 10 2020-04-28 08:00:00     Wx2       DewPoint     50.5000 Actual   49.99000
# 11 2020-04-28 08:00:00     Wx2      WindSpeed      7.3200 Actual    7.24000
# 12 2020-04-28 08:00:00     Wx2 SolarRadiation     19.0200 Actual   23.40000

推荐阅读