首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以一种更有效的方式使用geosphere::distm?

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

Stack Overflow用户
提问于 2020-01-18 03:36:31
回答 1查看 95关注 0票数 0

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

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-18 08:55:35

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

代码语言:javascript
复制
#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的子集创建多个较小的矩阵。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59793833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档