我的固定数据收集点很少,而且发生点很少。我需要根据距离阈值确定一个子集(在这种情况下,10英里)。
### Occurrence Points
set2 <- read.table(header=TRUE, text="
ID Latitude Longitude
AB1 30.16705556 97.78689167
AB2 30.17369444 97.8238
AB3 30.18354444 97.84982778
AB4 30.19023333 97.77014722
AB5 30.19064444 97.77090278
AB6 30.19818056 97.76365278
AB7 30.19867778 97.76448056
AB8 30.20056111 97.86548056
AB9 30.20061667 97.87836389
AB10 30.20095833 97.86684722
")
### Data Collection Points
set1 <- read.table(header=TRUE, text="
ID_IN lat long
IN1 35.195611 -101.866244
IN2 35.21335602 -101.9019775
IN3 32.70839722 97.115975
IN4 32.7123 97.06079722
IN5 32.71231667 97.06482778
IN6 32.75909722 97.09717778
IN7 32.75909722 97.10194722
IN8 32.75935833 97.11451389
")
## Function to calculate the distance:
require(fields)
distMatrix <- lapply(1:nrow(set1),function(x) {
#Find distances to all points
distances <- rdist.earth(set1[x,c('long','lat')], set2[,c('long','lat')], miles = TRUE)
return(distances)
})
distMatrix <- do.call(rbind,distMatrix)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 7752.1565 7751.0628 7749.9496 7750.9221 7750.8815 7750.5120 7750.4645 7748.5474 7748.3144 7748.4968
[2,] 7750.3569 7749.2624 7748.1486 7749.1231 7749.0825 7748.7133 7748.6657 7746.7462 7746.5129 7746.6956
[3,] 180.1959 180.2398 179.9400 178.4148 178.3966 177.7950 177.7719 179.0226 179.2081 179.0159
[4,] 181.2013 181.2833 181.0116 179.4096 179.3923 178.7854 178.7632 180.1145 180.3126 180.1093
[5,] 181.1463 181.2255 180.9517 179.3554 179.3380 178.7315 178.7093 180.0531 180.2503 180.0477
[6,] 183.8588 183.9059 183.6080 182.0766 182.0586 181.4564 181.4334 182.6919 182.8782 182.6853
[7,] 183.7967 183.8405 183.5402 182.0155 181.9974 181.3957 181.3726 182.6224 182.8075 182.6156
[8,] 183.6528 183.6878 183.3810 181.8741 181.8557 181.2553 181.2320 182.4585 182.6407 182.4514
这些价值似乎不对。
我希望得到一个基于阈值距离(10英里)的子集,如下所示:
ID Latitude Longitude ID_IN Dist
1 AB1 30.16706 97.78689 IN1 1.6
2 AB1 30.16706 97.78689 IN4 3.4
3 AB1 30.16706 97.78689 IN7 7.9
4 AB1 30.16706 97.78689 IN8 8.8
5 AB2 30.17369 97.82380 IN2 0.4
我正在尝试使用这两个线程到达那里;但仍然没有成功。