我有一些包含某些位置的数据集:
ex <- data.frame(lat = c(55, 60, 40), long = c(6, 6, 10))
Run Code Online (Sandbox Code Playgroud)
而且我有气候数据
clim <- structure(list(lat = c(55.047, 55.097, 55.146, 55.004, 55.054,
55.103, 55.153, 55.202, 55.252, 55.301), long = c(6.029, 6.0171,
6.0051, 6.1269, 6.1151, 6.1032, 6.0913, 6.0794, 6.0675, 6.0555
), alt = c(0.033335, 0.033335, 0.033335, 0.033335, 0.033335,
0.033335, 0.033335, 0.033335, 0.033335, 0.033335), x = c(0, 0,
0, 0, 0, 0, 0, 0, 0, 0), y = c(1914, 1907.3, 1901.8, 1921.1,
1914.1, 1908.3, 1902.4, 1896, 1889.8, 1884)), row.names = c(NA,
10L), class = "data.frame", .Names = c("lat", "long", "alt",
"x", "y"))
lat long alt x y
1 55.047 6.0290 0.033335 0 1914.0
2 55.097 6.0171 0.033335 0 1907.3
3 55.146 6.0051 0.033335 0 1901.8
4 55.004 6.1269 0.033335 0 1921.1
5 55.054 6.1151 0.033335 0 1914.1
6 55.103 6.1032 0.033335 0 1908.3
7 55.153 6.0913 0.033335 0 1902.4
8 55.202 6.0794 0.033335 0 1896.0
9 55.252 6.0675 0.033335 0 1889.8
10 55.301 6.0555 0.033335 0 1884.0
Run Code Online (Sandbox Code Playgroud)
我想要做的是"合并"两个数据集,以便在ex文件中包含气候数据.的价值lat和 long在ex比的值不同lat,并long在clim等我,他们不能直接合并(它是相同的long).我需要找到最好的点(在最近点clim的每一行中ex同时考虑lat和long)
该示例的预期输出是:
lat long alt x y
1 55 6 0.033335 0 1914.0
2 60 6 0.033335 0 1884.0
3 40 10 0.033335 0 1921.1
Run Code Online (Sandbox Code Playgroud)
您可以找到 中和中clim的绝对差值最小的行索引,然后根据该索引添加到列中。latlongexclimex
import(tidyverse)
ex %>%
group_by(lat, long) %>%
summarise(closest_clim = which.min(abs(lat - clim$lat) +
abs(long - clim$long))) %>%
mutate(alt = clim$alt[closest_clim],
x = clim$x[closest_clim],
y = clim$y[closest_clim])
# A tibble: 3 x 6
# Groups: lat [3]
lat long closest_clim alt x y
<dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 40. 10. 4 0.0333 0. 1921.
2 55. 6. 1 0.0333 0. 1914.
3 60. 6. 10 0.0333 0. 1884.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74 次 |
| 最近记录: |