使用dplyr :: mutate计算到数据点的地理距离

时间:2017-08-22 00:16:43

标签: r dplyr tidyverse geosphere

我正在尝试将R与 tidyverse 包一起使用,并且无法将函数应用于我的数据。我的数据包括纬度/经度坐标,我想计算从每个位置(我的数据帧的行)到参考位置的距离。我正在尝试使用 geosphere :: distm 函数。

library(tidyverse)
library(geosphere)

my_long <- 172
my_lat <- -43

data <- data %>%  rowwise() %>% mutate(
  dist = distm(c(myLong, myLat), c(long, lat), fun=distHaversine) # this works
)

我使用rowwise()函数使用它,如上所述,但这已被弃用,因此我想知道如何使用现代tidyverse,即dplyr或{ {1}},我认为,例如我最接近的是使用purrr

map2:

到目前为止,我失败了。

4 个答案:

答案 0 :(得分:6)

您可以将mutatemapply

一起使用
library(tidyverse)
library(geosphere)

my_long <- 172
my_lat <- -43

df <- data.frame(long = c(170, 180), lat = c(-43, 43))
df %>% rowwise() %>% mutate(
  dist = distm(c(my_long, my_lat), c(long, lat), fun=distHaversine) # this works
)

#Source: local data frame [2 x 3]
#Groups: <by row>

# A tibble: 2 x 3
#   long   lat    dist
#  <dbl> <dbl>   <dbl>
#1   170   -43  162824
#2   180    43 9606752

df %>% mutate(
    dist = mapply(function(lg, lt) distm(c(my_long, my_lat), c(lg, lt), fun=distHaversine), long, lat)
)

#  long lat    dist
#1  170 -43  162824
#2  180  43 9606752

使用map2更新

df %>% 
    mutate(dist = map2(long, lat, ~distm(c(my_long, my_lat), c(.x, .y), fun=distHaversine)))
# here .x stands for a value from long column, and .y stands for a value from lat column
#  long lat    dist
#1  170 -43  162824
#2  180  43 9606752

使用my_distm

my_distm <- function(long1, lat1, long2, lat2)
    distm(c(long1, lat1), c(long2, lat2), fun=distHaversine)

df %>% mutate(dist = map2(long, lat, ~my_distm(my_long, my_lat, .x, .y)))
#  long lat    dist
#1  170 -43  162824
#2  180  43 9606752

答案 1 :(得分:4)

您可以使用distHaversine代替distmcbind

data %>%  mutate(dist = distHaversine(cbind(myLong, myLat), cbind(long, lat)))

示例数据:

myLong = 172
myLat = -43 
long = c(180,179,179)
lat = c(-40,-41,-40)
data = data.frame(myLong,myLat,long,lat)

结果如下:

  myLong myLat long lat     dist
1    172   -43  180 -40 745481.0
2    172   -43  179 -41 620164.8
3    172   -43  179 -40 672076.2

答案 2 :(得分:2)

我也非常喜欢rowwise,但是因为你正在寻找其他解决方案

Psidom

的数据
my_long <- 172
my_lat <- -43
myval <- c(my_long, my_lat)

df <- data.frame(long = c(170, 180), lat = c(-43, 43))

purrr solution

此处purrr::map

library(purrr)
df1 <- df %>%  
         mutate(dist = map(1:nrow(.), ~distm(myval, df[.x,], fun=distHaversine)))

#   long lat    dist
# 1  170 -43  162824
# 2  180  43 9606752

您可以使用map2多次重复myval到2列data.frame的形状,但不能作为向量

OP 的

请求

要从较大的数据框中选择longlat以用于distm,请在select声明中使用map

garbage <- data.frame(long = c(170, 180), lat = c(-43, 43), junk=c(0,0))
df1 <- garbage %>%  
         mutate(dist = map(1:nrow(.), ~distm(myval, select(garbage[.x,],long,lat), fun=distHaversine)))

#   long lat junk    dist
# 1  170 -43    0  162824
# 2  180  43    0 9606752
带有迭代器的

sapply解决方案

我也想将iterators用于行方向操作

library(iterators)
df2 <- df %>%
         mutate(dist = sapply(iter(df, by="row"), function(x) distm(myval, x, fun=distHaversine)))

#   long lat    dist
# 1  170 -43  162824
# 2  180  43 9606752

答案 3 :(得分:2)

您可以使用pmap()

f  <- function(StartLong, StartLat, EndLong, EndLat) 
  distm(c(StartLong, StartLat), c(EndLong, EndLat))

df %>% mutate(dist = pmap_dbl(., f))

Vectorize()您的功能并直接在mutate()中使用:

g <- Vectorize(f)
df %>% mutate(dist = g(StartLong, StartLat, EndLong, EndLat))

给出了:

#  StartLong StartLat EndLong EndLat    dist
#1       170      -43     172    -43  162824
#2       180       43     172    -43 9606752

来自by_row()

purrrlyr的另一个想法
library(purrrlyr)

df %>%
  by_row(function(x) {
    distm(c(x$StartLong, x$StartLat), 
          c(x$EndLong, x$EndLat)) },
    .collate = "rows", .to = "dist") 

给出了:

## tibble [2 x 5]
#  StartLong StartLat EndLong EndLat    dist
#      <dbl>    <dbl>   <dbl>  <dbl>   <dbl>
#1       170      -43     172    -43  162824
#2       180       43     172    -43 9606752

数据

df <- structure(list(StartLong = c(170, 180), StartLat = c(-43, 43), 
      EndLong = c(172, 172), EndLat = c(-43, -43)), .Names = c("StartLong", 
      "StartLat", "EndLong", "EndLat"), row.names = c(NA, -2L), class = "data.frame")