计算一定半径内的点数 [英] Calculating number of points within a certain radius

查看:92
本文介绍了计算一定半径内的点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个具有经度和纬度的数据框,我想添加一列,该列仅包含在一定半径范围内(同一数据框)的其他点的计数.在该特定点的10公里之内.

Given a data frame with latitudes and longitudes I want to add a column that simply contains the count of other points (of the same data frame) that are within a certain radius e.g. within 10 km of that specific point.

示例数据:

set.seed(1)
radius<-10
lat<-runif(10,-90,90)
long<-runif(10,-180,180)
id<-1:10
dat<-cbind(id,lat,long)

      id       lat         long
 [1,]  1 -42.20844 -105.8491530
 [2,]  2 -23.01770 -116.4395691
 [3,]  3  13.11361   67.3282248
 [4,]  4  73.47740  -41.7226614
 [5,]  5 -53.69725   97.1429112
 [6,]  6  71.71014   -0.8282728
 [7,]  7  80.04155   78.3426630
 [8,]  8  28.94360  177.0861941
 [9,]  9  23.24053  -43.1873354
[10,] 10 -78.87847   99.8802797

现在给定半径变量,我想要一个新列说"X",该列的每个点仅包含半径"内的其他点的数量.我不在乎这些是什么.

Now given the radius variable I want a new column say "X" that for each point only contains the number of other points that are within "radius". I do not care about which points these are.

这是

While this R - Finding closest neighboring point and number of neighbors within a given radius, coordinates lat-long topic and answer get close it does not solve the specific question of the simple count. This question is different as I need the counts of all points within the radius and not the points

推荐答案

尝试一下:

library(geosphere)
cbind(dat, X=rowSums(distm (dat[,3:2], 
       fun = distHaversine) / 1000 <= 10000)) # number of points within distance 10000 km

      id       lat         long X
 [1,]  1 -42.20844 -105.8491530 5
 [2,]  2 -23.01770 -116.4395691 5
 [3,]  3  13.11361   67.3282248 5
 [4,]  4  73.47740  -41.7226614 6
 [5,]  5 -53.69725   97.1429112 4
 [6,]  6  71.71014   -0.8282728 6
 [7,]  7  80.04155   78.3426630 6
 [8,]  8  28.94360  177.0861941 5
 [9,]  9  23.24053  -43.1873354 6
[10,] 10 -78.87847   99.8802797 4

这篇关于计算一定半径内的点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆