R 在每个组中查找最近的地理点

R 在每个组中查找最近的地理点,r,distance,spatial,R,Distance,Spatial,我试图为每个“年”组中的每个银行找到最近的点(在我的例子中是最近的银行)。我尝试过使用st_distance,但没有成功 以下是我的数据示例: Year geometry bank Location 7 1838 POINT (759859.6 -728345) Bank of Mobile Mobile 43 1838 POIN

我试图为每个“年”组中的每个银行找到最近的点(在我的例子中是最近的银行)。我尝试过使用st_distance,但没有成功

以下是我的数据示例:

   Year                   geometry                                 bank   Location
7  1838   POINT (759859.6 -728345)                       Bank of Mobile     Mobile
43 1838 POINT (779861.1 -445454.7)         Bank of the State of Alabama Tuscaloosa
58 1838 POINT (819114.6 -285180.1) Bank of the State of Alabama, branch    Decatur
59 1841 POINT (819114.6 -285180.1) Bank of the State of Alabama, branch    Decatur
60 1842 POINT (819114.6 -285180.1) Bank of the State of Alabama, branch    Decatur
67 1838 POINT (853709.4 -267830.7) Bank of the State of Alabama, branch Huntsville
68 1841 POINT (853709.4 -267830.7) Bank of the State of Alabama, branch Huntsville
69 1842 POINT (853709.4 -267830.7) Bank of the State of Alabama, branch Huntsville
79 1838   POINT (759859.6 -728345) Bank of the State of Alabama, branch     Mobile
91 1838 POINT (905601.7 -526517.9) Bank of the State of Alabama, branch Montgomery
我想知道的是,比如说,1838年、1841年和1842年分别与“迪凯特”的“阿拉巴马州银行分行”最接近的银行是什么

对于一些银行来说,与另一家银行的最小距离应为零,因为同一城镇/城市中有多家银行(例如1838年的“移动银行”)


以下是一个可复制的示例:

  S1 <- structure(list(Year = c("1838", "1838", "1838", "1841", "1842", 
    "1838", "1841", "1842", "1838", "1838", "1841", "1842", "1838"
    ), geometry = structure(list(structure(c(759859.587499541, -728344.968108917
    ), class = c("XY", "POINT", "sfg")), structure(c(779861.05153977, 
    -445454.703808866), class = c("XY", "POINT", "sfg")), structure(c(819114.617375314, 
    -285180.101652618), class = c("XY", "POINT", "sfg")), structure(c(819114.617375314, 
    -285180.101652618), class = c("XY", "POINT", "sfg")), structure(c(819114.617375314, 
    -285180.101652618), class = c("XY", "POINT", "sfg")), structure(c(853709.422752713, 
    -267830.684434561), class = c("XY", "POINT", "sfg")), structure(c(853709.422752713, 
    -267830.684434561), class = c("XY", "POINT", "sfg")), structure(c(853709.422752713, 
    -267830.684434561), class = c("XY", "POINT", "sfg")), structure(c(759859.587499541, 
    -728344.968108917), class = c("XY", "POINT", "sfg")), structure(c(905601.700907393, 
    -526517.946820037), class = c("XY", "POINT", "sfg")), structure(c(759859.587499541, 
    -728344.968108917), class = c("XY", "POINT", "sfg")), structure(c(759859.587499541, 
    -728344.968108917), class = c("XY", "POINT", "sfg")), structure(c(336877.092499058, 
    -301769.66275037), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", 
    "sfc"), precision = 0, bbox = structure(c(xmin = 336877.092499058, 
    ymin = -728344.968108917, xmax = 905601.700907393, ymax = -267830.684434561
    ), class = "bbox"), crs = structure(list(epsg = NA_integer_, proj4string = "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=37.5 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"), class = "crs"), n_empty = 0L), 
    bank = c("Bank of Mobile", "Bank of the State of Alabama", 
    "Bank of the State of Alabama, branch", "Bank of the State of Alabama, branch", 
    "Bank of the State of Alabama, branch", "Bank of the State of Alabama, branch", 
    "Bank of the State of Alabama, branch", "Bank of the State of Alabama, branch", 
    "Bank of the State of Alabama, branch", "Bank of the State of Alabama, branch", "Planters & Merchants Bank", "Planters & Merchants Bank",
    "Bank of the State of Arkansas"), Location = c("Mobile", 
    "Tuscaloosa", "Decatur", "Decatur", "Decatur", "Huntsville", 
    "Huntsville", "Huntsville", "Mobile", "Montgomery", "Mobile", 
    "Mobile", "Little Rock")), sf_column = "geometry", agr = structure(c(Year = NA_integer_, 
    bank = NA_integer_, Location = NA_integer_), class = "factor", .Label = c("constant", 
    "aggregate", "identity")), row.names = c(7L, 43L, 58L, 59L, 60L, 
    67L, 68L, 69L, 79L, 91L, 116L, 117L, 130L), class = c("sf", "data.frame"
    ))

S1这是一个具有参数的函数,允许您按年份、银行名称和位置选择银行。它将返回一个
sf
对象,该对象具有该位置和data.frame中最近的位置,该data.frame来自同一年或以后年份,具有不同的名称和位置

您可能需要更改index2的逻辑,具体取决于您要查找的内容

dist_by_year <- function(x = S1, year = 1841, 
                         bank = 'Bank of the State of Alabama, branch',
                         loc = 'Decatur'){
  x$Year <- as.numeric(x$Year)
  index1 <- which(x$bank == bank & x$Year == year & x$Location == loc)
  
  index2 <- which((x$bank != bank & x$Location != loc)& x$Year >= year )
  
  
  index3 <- st_nearest_feature(x[index1,], x[index2,])
  
  closest <- x[index2,][index3,]
  closest$id <- 'closest'
  target <- x[index1,]
  target$id <- 'target'
  rbind(closest, target)

}

可通过管道连接到st_distance
以获取实际距离:

> dist_by_year() %>% st_distance()
Units: [m]
         [,1]     [,2]
[1,]      0.0 447108.8
[2,] 447108.8      0.0

这是一个具有参数的函数,允许您按年份、银行名称和位置选择银行。它将返回一个
sf
对象,该对象具有该位置和data.frame中最近的位置,该data.frame来自同一年或以后年份,具有不同的名称和位置

您可能需要更改index2的逻辑,具体取决于您要查找的内容

dist_by_year <- function(x = S1, year = 1841, 
                         bank = 'Bank of the State of Alabama, branch',
                         loc = 'Decatur'){
  x$Year <- as.numeric(x$Year)
  index1 <- which(x$bank == bank & x$Year == year & x$Location == loc)
  
  index2 <- which((x$bank != bank & x$Location != loc)& x$Year >= year )
  
  
  index3 <- st_nearest_feature(x[index1,], x[index2,])
  
  closest <- x[index2,][index3,]
  closest$id <- 'closest'
  target <- x[index1,]
  target$id <- 'target'
  rbind(closest, target)

}

可通过管道连接到st_distance以获取实际距离:

> dist_by_year() %>% st_distance()
Units: [m]
         [,1]     [,2]
[1,]      0.0 447108.8
[2,] 447108.8      0.0

以下是我如何解决这个问题的,以供将来参考。它给出了到10个最近点(气缸组)的距离:

for(我在1830:1860){

d以下是我解决问题的方法,供将来参考。它给出了到10个最近点(银行)的距离:

for(我在1830:1860){

你用的是什么坐标系?我用的是“阿尔伯斯等面积圆锥投影”,虽然我认为我需要改变它,因为我需要的是距离,而不是面积来保存。(CRS(“+proj=aea+lat_1=29.5+lat_2=45.5+lat_0=37.5+lon_0=-96+x_0=0+y_0=0+datum=NAD83+units=m+no_defs”))你用的是什么坐标系?我用的“阿尔伯斯等面积圆锥投影”虽然我认为我需要改变这一点,因为我需要的是距离,而不是面积(CRS(“+proj=aea+lat_1=29.5+lat_2=45.5+lat_0=37.5+lon_0=-96+x_0=0+y_0=0+datum=NAD83+units=m+no_defs”))谢谢你的回答。这很有意义。我将如何使用你的解决方案来计算每次观测的最小距离?我相信一定有一种方法可以自动计算,但我在R还是很新。因为我有29000多个观测,所以一个接一个地做是不可能的。Purr::map可能会有帮助。谢谢你的回答。这很有帮助有道理。我将如何使用您的解决方案来计算每次观测的最小距离?我相信一定有办法实现自动化,但我在R还是相当新的。因为我有29000多个观测,所以逐个进行是不可能的。purrr::map可能会有所帮助。