以更有效的方式使用geosphere :: distm?

问题描述 投票:0回答:1

使用商店的位置数据,我正在尝试查找“竞争者”-定义为一定距离内的其他商店。

我正在使用geo sphere::distm和下面的一些矩阵运算。问题是我的矩阵很大(100,000 X 100,000),并且花费的时间很长(或者我的内存不支持这种类型的分析)。有没有办法使下面的代码更高效?输入文件看起来像locations_data(但更大)。所需的输出是数据表competitors,其中每行包含一对竞争者。我是用R语言编写高效代码的新手,想寻求帮助。

locations_data<-cbind(id=1:100, longitude=runif(100,min=-180, max=-120), latitude=runif(100, min=50, max=85))

#require(geosphere)
mymatrix<-distm(locations_data[,2:3])

#require(data.table)
analyze_competitors<-function(mymatrix){
    mymatrix2<-matrix(as.numeric(mymatrix<1000000), nrow(mymatrix), ncol(mymatrix)) #
    competitors<-which(mymatrix2==1,arr.ind = T)
    competitors<-data.table(competitors)
    return(competitors)
}

competitors<-analyze_competitors(mymatrix)
r matrix rcpp geosphere
1个回答
0
投票
#converting your example data to a tibble. locations_data<-tibble::as_tibble(locations_data) #create a numeric grid spanning the extent of your latitude and longitude locations_data$long_quant<-findInterval(locations_data$longitude, quantile(locations_data$longitude,probs = seq(0,1,.2)), rightmost.closed=TRUE) locations_data$lat_quant<-findInterval(locations_data$latitude, quantile(locations_data$latitude,probs = seq(0,1,.2)), rightmost.closed=TRUE)

然后您可以使用locations_data的子集创建多个较小的矩阵。

© www.soinside.com 2019 - 2024. All rights reserved.