繁体   English   中英

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

[英]Use the geosphere::distm in a more efficient way?

使用商店的位置数据,我试图找到“竞争对手”——它被定义为一定距离内的其他商店。

我正在使用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)

如果您想要较小的矩阵,请考虑使用基于经度和/或纬度的网格拆分数据。 例如,这会生成两个带有 5 x 5 网格标签的新列。

#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 的子集创建多个较小的矩阵。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM