搜索和匹配两个数据框

时间:2016-04-27 12:20:13

标签: r search data-manipulation

我有两个数据帧,数据帧的输出如下:

dput(DF1)

structure(list(observationtime = structure(c(0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), format = "h:m:s", class = "times"), observationdate = structure(c(15309, 
15309, 15309, 15309, 15309, 15309, 15309, 15309, 15309, 15309, 
15309, 15309, 15309, 15309, 15309, 15309, 15309, 15309, 15309, 
15309), class = "Date"), screentemperature = c(9.1, 9.4, 6.8, 
5.7, 1.6, 6.3, 5.2, 4.7, 6.8, 8.7, 6.9, 9.6, 9.4, 9.5, 8.1, 7.7, 
7.9, 8.9, 6.6, 6.8), significantweathercode = c(8L, 7L, 8L, 9L, 
15L, 12L, 12L, 15L, 15L, 9L, 15L, 7L, 2L, 7L, 12L, 8L, 12L, 8L, 
8L, 12L), latitude = c(60.139, 58.954, 57.358, 58.214, 57.725, 
57.859, 57.257, 58.288, 56.867, 57.82, 57.206, 58.454, 57.6494, 
57.712, 57.077, 56.85, 57.206, 57.698, 56.497, 55.681), longitude = c(-1.183, 
-2.9, -7.397, -6.325, -4.896, -5.636, -5.809, -4.442, -4.708, 
-3.97, -3.827, -3.089, -3.5606, -3.322, -2.836, -2.27, -2.202, 
-2.121, -6.887, -6.256)), .Names = c("observationtime", "observationdate", 
"screentemperature", "significantweathercode", "latitude", "longitude"
), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 8L, 10L, 11L, 12L, 13L, 
14L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L), class = "data.frame")

dput(DF2)

structure(list(time = structure(c(0.0104166666666667, 0.0590277777777778
), format = "h:m:s", class = "times"), data = structure(c(15309, 
15310), class = "Date"), latitude = c(53.674, 43.978), longitude = c(-1.222, 
-5.346)), .Names = c("time", "data", "latitude", "longitude"), row.names = c(NA, 
-2L), class = "data.frame")

我正在尝试使用df1中的值从df2中提取温度和重要天气码。例如,df2的日期将与df1完全匹配,但时间可能不一样,所以我需要确定15-30分钟的阈值才能找到最接近的时间,同时我需要匹配坐标可能不完全匹配,并且需要阈值才能找到最接近的匹配。步骤是:

  • 匹配日期
  • 匹配位置/坐标,如果不完全匹配,请找到最近的
  • 匹配时间,如果不完全匹配,则找到最接近的时间

我的解决方案是

  • 根据日期
  • 从df1子集化数据
  • 选择具有最小时差和最小距离的观察
  • 找出时间和坐标之间的差异

实际上,数据很大,每个数据帧包含近50万行。我试图在r中找到一种有效的方法来尝试进行这样的匹配。

欢迎提出可行的建议。

1 个答案:

答案 0 :(得分:0)

数据库解决方案比纯R解决方案更有效。例如。具有PostGIS扩展的PostgreSQL数据库将允许空间查询。