[英]Apply a function to each row and the subsequent row by group
我有一個 sf dataframe 包含標記許多單向街道交叉口位置的點。 除了幾何列之外,一列包含街道名稱,另一列包含單向街道上交叉口的相對 position。
下面是一個玩具示例。 第一排是拱街的第一個路口,第二排是拱街的第二個路口,以此類推。
library(sf)
intersections <- structure(list(street = c("ARCH ST", "ARCH ST", "ARCH ST", "SANSOM ST",
"SANSOM ST", "SANSOM ST"), number = c(1L, 2L, 3L, 1L, 2L, 3L),
geometry = structure(list(structure(c(2699665.2606043, 236074.947200272
), class = c("XY", "POINT", "sfg")), structure(c(2699402.74765515,
236109.729280198), class = c("XY", "POINT", "sfg")), structure(c(2699202.95996668,
236136.613760229), class = c("XY", "POINT", "sfg")), structure(c(2699431.38476158,
234437.663731016), class = c("XY", "POINT", "sfg")), structure(c(2699162.09261096,
234476.514355583), class = c("XY", "POINT", "sfg")), structure(c(2697100.77148795,
234809.605567052), class = c("XY", "POINT", "sfg"))), precision = 0, bbox = structure(c(xmin = 2697100.77148795,
ymin = 234437.663731016, xmax = 2699665.2606043, ymax = 236136.613760229
), class = "bbox"), crs = structure(list(epsg = 2272L, proj4string = "+proj=lcc +lat_1=40.96666666666667 +lat_2=39.93333333333333 +lat_0=39.33333333333334 +lon_0=-77.75 +x_0=600000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs"), class = "crs"), n_empty = 0L, class = c("sfc_POINT",
"sfc"))), row.names = c(NA, -6L), class = c("sf", "tbl_df",
"tbl", "data.frame"), sf_column = "geometry", agr = structure(c(street = NA_integer_,
number = NA_integer_), class = "factor", .Label = c("constant",
"aggregate", "identity")))
> intersections
Simple feature collection with 6 features and 2 fields
geometry type: POINT
dimension: XY
bbox: xmin: 2697101 ymin: 234437.7 xmax: 2699665 ymax: 236136.6
epsg (SRID): 2272
proj4string: +proj=lcc +lat_1=40.96666666666667 +lat_2=39.93333333333333 +lat_0=39.33333333333334 +lon_0=-77.75 +x_0=600000 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs
# A tibble: 6 x 3
street number geometry
<chr> <int> <POINT [US_survey_foot]>
1 ARCH ST 1 (2699665 236074.9)
2 ARCH ST 2 (2699403 236109.7)
3 ARCH ST 3 (2699203 236136.6)
4 SANSOM ST 1 (2699431 234437.7)
5 SANSOM ST 2 (2699162 234476.5)
6 SANSOM ST 3 (2697101 234809.6)
使用 mapsapi package 中的mapsapi
mp_matrix()
和mp_get_matrix()
,我想添加一列,顯示從每個交叉口到該街道上下一個交叉口的行駛時間(最后一個交叉口除外,它獲得 NA)。
理想情況下,它如下所示:
street number travel_time_sec geometry
1 ARCH ST 1 210 POINT (2699665 236074.9)
2 ARCH ST 2 180 POINT (2699403 236109.7)
3 ARCH ST 3 NA POINT (2699203 236136.6)
4 SANSOM ST 1 150 POINT (2699431 234437.7)
5 SANSOM ST 2 175 POINT (2699162 234476.5)
6 SANSOM ST 3 NA POINT (2697101 234809.6)
如何按組(即街道)遍歷 sf dataframe 中的行(即街道),告訴每一行對該組中的下一行執行操作以填充新列,如果不存在這樣的下一行,則返回 NA?
最后,由於mp_matrix()
調用 Google Maps API,這需要花錢,請改用st_distance()
function 從sf
生成以下內容。
street number travel_distance geometry
1 ARCH ST 1 576 POINT (2699665 236074.9)
2 ARCH ST 2 397 POINT (2699403 236109.7)
3 ARCH ST 3 NA POINT (2699203 236136.6)
4 SANSOM ST 1 410 POINT (2699431 234437.7)
5 SANSOM ST 2 440 POINT (2699162 234476.5)
6 SANSOM ST 3 NA POINT (2697101 234809.6)
非常感謝您的幫助。
我在玩您的示例,但無法使用st_distance
function 獲得相同的travel distance
。
st_distance(intersections$geometry[1], intersections$geometry[2])
Units: [US_survey_foot]
[,1]
[1,] 264.8072
通過行本身的循環或矢量化操作可以用這段代碼完成
# used librarys
library(units)
library(tidyverse)
library(sf)
# find distance function
find_Distance <- function(x) {
# create lead list
x_lead <- x[2:length(x)]
# create distance matrix
distance_matrix <- st_distance(x, x_lead)
# diagonal of the distance matrix is your desired output, fill last entry with NA and
# unit
c(diag(distance_matrix), set_units(NA, "US_survey_foot"))
}
# group by street and calculate distance
intersections <- group_by(intersections, street) %>%
mutate(travel_distance = find_Distance(geometry))
# if needed, set unit of travel distance
units(intersections$travel_distance) <- as_units("US_survey_foot")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.