我有一个有488个GPS点(长的和晚的)的数据帧.对于每488分,我想找出他们最接近的两个邻居.

到目前为止,我已经创建了一个点模式对象,并计算了到最近的两个点(如下所示)的距离.然而,我想更进一步,能够从原始数据集中通过ID识别这些最近的点.

目前,我的脚本工作方式如下:

# 1. store x and y coords in two vectors
lon <- data$longitude
lat <- data$latitude

# 2. create two vectors xrange and yrange with dimensions of triangle that contain all points
xrange <- range(lon, na.rm=T)
yrange <- range(lat, na.rm=T)

# 3. create ppp
lf <- ppp(lon, lat, xrange, yrange)

plot(lf)

nndist(lf, k = 1:2)

给我(前5名结果的例子):

             dist.1       dist.2
  [1,] 1.426925e-03 0.0017007414
  [2,] 1.017287e-03 0.0015574895
  [3,] 6.502012e-04 0.0010172867
  [4,] 6.502012e-04 0.0007202307
  [5,] 7.202307e-04 0.0010472445
 

但我希望能够将它链接回原始数据集的"HID",如下所示:

  hhid         dist.1  dist.1.hhid         dist.2     dist.1.hhid
  1    1.426925e-03             7  0.0017007414                 3
  2    1.017287e-03             6  0.0015574895                 4
  3    6.502012e-04            10  0.0010172867                 5
  4    6.502012e-04             2  0.0007202307                 8
  5    7.202307e-04             1  0.0010472445                13

原始数据集的前20行:

structure(list(hhid = c(2004L, 2006L, 2009L, 2012L, 2013L, 2020L, 
2022L, 2023L, 2028L, 2029L, 2035L, 2036L, 2043L, 2046L, 2047L, 
2059L, 2062L, 2063L, 2065L, 2066L), longitude = c(-1.478302479, 
-1.477469802, -1.476488709, -1.476146936, -1.47547996, -1.475799441, 
-1.475903392, -1.476232767, -1.476053953, -1.477196693, -1.476906657, 
-1.478778243, -1.480723381, -1.433436394, -1.433033824, -1.428791046, 
-1.431989908, -1.432058454, -1.43134892, -1.430848002), latitude = c(12.10552216, 
12.10700512, 12.10673618, 12.10618305, 12.10645485, 12.10846806, 
12.1080761, 12.10830975, 12.11114883, 12.11076546, 12.11197853, 
12.11345387, 12.10725021, 12.1183548, 12.11699867, 12.11466122, 
12.1154108, 12.11545277, 12.11554337, 12.11567497)), row.names = c(NA, 
20L), class = "data.frame")

推荐答案

这似乎是here个问题的延伸.从这个问题的accepted answer扩展到你的特定情况,判断最近的两个邻居,你可以这样做:

library(sp)
library(rgeos)
# dput structure in question assigned as "df"

spatialDF <- df
coordinates(spatialDF) <- ~longitude + latitude
dists <- gDistance(spatialDF, byid = TRUE)
min.2dists <- apply(dists, 1, function(x) order(x, decreasing = FALSE)[2:3])

# closest
df$hhid1 <- df[min.2dists[1,],"hhid"]
df$dist1 <- apply(dists, 1, function(x) sort(x, decreasing = FALSE)[2])

# second closest
df$hhid2 <- df[min.2dists[2,],"hhid"]
df$dist2 <- apply(dists, 1, function(x) sort(x, decreasing = FALSE)[3])

输出:

#    hhid longitude latitude hhid1        dist1 hhid2        dist2
# 1  2004 -1.478302 12.10552  2006 1.700741e-03  2009 0.0021825687
# 2  2006 -1.477470 12.10701  2009 1.017287e-03  2012 0.0015574895
# 3  2009 -1.476489 12.10674  2012 6.502012e-04  2006 0.0010172867
# 4  2012 -1.476147 12.10618  2009 6.502012e-04  2013 0.0007202307
# 5  2013 -1.475480 12.10645  2012 7.202307e-04  2009 0.0010472445
# ...

R相关问答推荐

R中的枢轴/转置

提取R中值和列名的所有可能组合

ggplot 2中的地块底图(basemaps_gglayer()不起作用)

以R中的正确顺序将日期时间字符列转换为posixct

如何使用R中的dhrr函数将李克特量表的因子列从长转换为宽?

判断字符串中数字的连续性

标识R中多个列中缺少的唯一值

如何在ggplot图中找到第二轴的比例

使用across,starts_with和ifelse语句变更多个变量

为了网络分析目的,将数据框转换为长格式列联表

从非重叠(非滚动)周期中的最新数据向后开窗并在周期内计数

在数据帧列表上绘制GGPUP

如何将一列中的值拆分到R中各自的列中

为什么函数toTitleCase不能处理english(1),而toupper可以?

Rmarkdown::Render vs Source()

有没有办法通过str_Detect()或其他字符串匹配函数来连接两个长度不等的数据帧?

按两个因素将观测值分组后计算单独的百分比

以R表示的NaN值的IS.NA状态

如何为包创建自定义roxygen2标签?

Package emMeans:如果emmip模型中包含的变量较少,emMeans模型中的其他变量设置为什么?