对于表中的每个观测值,根据纬度和经度(R),计算x米范围内表中其他观测值的数量
我有一个带有经度和纬度的位置数据框,看起来像:对于表中的每个观测值,根据纬度和经度(R),计算x米范围内表中其他观测值的数量,r,loops,apply,latitude-longitude,geo,R,Loops,Apply,Latitude Longitude,Geo,我有一个带有经度和纬度的位置数据框,看起来像: set.seed(211) latitude <-runif(5000, min=50, max=55) longitude <- runif(5000, min=-2, max=0) location_id <- seq(1,5000) reprex <- data.frame(location_id, latitude, longitude) set.seed(211) 纬度使用distGeo函数可以实现,但您需要一
set.seed(211)
latitude <-runif(5000, min=50, max=55)
longitude <- runif(5000, min=-2, max=0)
location_id <- seq(1,5000)
reprex <- data.frame(location_id, latitude, longitude)
set.seed(211)
纬度使用distGeo
函数可以实现,但您需要一个循环。请注意,坐标的第一列必须是经度
lst <- vector(50, mode="list")
for(i in 1:50) {
dist <- distGeo(p1=reprex[i,c(3,2)], p2=reprex[-i,c(3,2)])
lst[[i]] <- sum(dist<16000)
}
reprex$n <- unlist(lst)
table(unlist(lst))
0 1 2
34 10 6
lstdistGeo
函数可以执行此操作,但您需要一个循环。请注意,坐标的第一列必须是经度
lst <- vector(50, mode="list")
for(i in 1:50) {
dist <- distGeo(p1=reprex[i,c(3,2)], p2=reprex[-i,c(3,2)])
lst[[i]] <- sum(dist<16000)
}
reprex$n <- unlist(lst)
table(unlist(lst))
0 1 2
34 10 6
lst字段
中的rdist.earth
函数似乎对此很有用,例如:
library(fields)
dist.matrix <- rdist.earth(reprex[-1])
colSums(dist.matrix<10)
库(字段)
dist.matrix字段
中的rdist.earth
函数对此似乎很有用,例如:
library(fields)
dist.matrix <- rdist.earth(reprex[-1])
colSums(dist.matrix<10)
库(字段)
dist.matrix将循环隐藏在(仍然很慢)应用中
并解开纬度和经度(它们通常是相反的方向),您可以尝试以下方法
set.seed(211)
latitude <-runif(5000, min=50, max=55)
longitude <- runif(5000, min=-2, max=0)
location_id <- seq(1, 5000)
reprex <- data.frame(location_id, latitude, longitude)
library(geosphere)
within10m <- function(p1, p2, dist=16000){
sum(geosphere::distGeo(p1, p2) <= dist)
}
matpoints <- as.matrix(reprex[, 3:2])
reprex$neighbours <-
apply(matpoints, 1, within10m, p2=matpoints) - 1
head(reprex)
# location_id latitude longitude neighbours
# 1 1 51.17399 -1.1489713 48
# 2 2 54.52623 -1.8554624 39
# 3 3 54.84852 -0.3014742 56
# 4 4 51.72104 -1.8644226 50
# 5 5 51.32793 -0.7417923 56
# 6 6 50.07346 -0.8939857 36
set.seed(211)
latitude将循环隐藏在一个(仍然很慢的)应用中
并解开纬度和经度(它们通常是相反的方向),您可以尝试以下方法
set.seed(211)
latitude <-runif(5000, min=50, max=55)
longitude <- runif(5000, min=-2, max=0)
location_id <- seq(1, 5000)
reprex <- data.frame(location_id, latitude, longitude)
library(geosphere)
within10m <- function(p1, p2, dist=16000){
sum(geosphere::distGeo(p1, p2) <= dist)
}
matpoints <- as.matrix(reprex[, 3:2])
reprex$neighbours <-
apply(matpoints, 1, within10m, p2=matpoints) - 1
head(reprex)
# location_id latitude longitude neighbours
# 1 1 51.17399 -1.1489713 48
# 2 2 54.52623 -1.8554624 39
# 3 3 54.84852 -0.3014742 56
# 4 4 51.72104 -1.8644226 50
# 5 5 51.32793 -0.7417923 56
# 6 6 50.07346 -0.8939857 36
set.seed(211)
latitude最终我在这里使用了答案,因为它非常优雅,避免了循环:
我使用了代码:
library(geosphere) # for distHaversine() and distm() functions
reprex <- cbind(reprex, # appends to the dataset...
count_nearby=rowSums( # ... a column which counts the rows in the dataset...
distm (reprex[,3:2], fun = distHaversine) # ... where the distance between current and other rows...
<= 16000)-1 # ... is less than 16000 metres. Take one away because it counts itself!
) #close the cbind brackets!
library(geosphere)#用于distHaversine()和distm()函数
reprex最终我在这里使用了答案,因为它非常优雅,避免了循环:
我使用了代码:
library(geosphere) # for distHaversine() and distm() functions
reprex <- cbind(reprex, # appends to the dataset...
count_nearby=rowSums( # ... a column which counts the rows in the dataset...
distm (reprex[,3:2], fun = distHaversine) # ... where the distance between current and other rows...
<= 16000)-1 # ... is less than 16000 metres. Take one away because it counts itself!
) #close the cbind brackets!
library(geosphere)#用于distHaversine()和distm()函数
reprex计算速度是这里的一个重要问题吗?对于您的问题,最简单的代码选项可能不会是最快的。若你们需要速度,那个么开发一种将你们的点分类的算法当然是值得的(想想你们地图上的网格模式)。如果栅格步长为10英里,则只需查看同一组或“邻居”组中的元素,而无需浏览每个点的整个地图。根据您拥有的点数及其密度,此优化的影响可能或多或少重要。计算速度是否是一个重要问题?对于您的问题,最简单的代码选项可能不会是最快的。若你们需要速度,那个么开发一种将你们的点分类的算法当然是值得的(想想你们地图上的网格模式)。如果栅格步长为10英里,则只需查看同一组或“邻居”组中的元素,而无需浏览每个点的整个地图。根据您拥有的点数及其密度,此优化的影响可能或多或少重要。