purrr map2说参数是不同的长度

时间:2018-11-09 17:01:16

标签: r purrr

尝试使用经纬度和map2来计算差异。

创建了一个reprex以显示错误。

为什么返回.x的数据帧的长度而不是数据帧中的行的长度

lat1 <- rnorm(100, 70, .1)
lon1 <- rnorm(100, 45, .1)
lat2 <- rnorm(1, 70, .1)
lon2 <- rnorm(1, 45, .1)


df <- tibble(lat1,lon1, lat2, lon2)
#> Error in tibble(lat1, lon1, lat2, lon2): could not find function "tibble"

earth.dist <- function (long1, lat1, long2, lat2)
{
  rad <- pi/180
  a1 <- lat1 * rad
  a2 <- long1 * rad
  b1 <- lat2 * rad
  b2 <- long2 * rad
  dlon <- b2 - a2
  dlat <- b1 - a1
  a <- (sin(dlat/2))^2 + cos(a1) * cos(b1) * (sin(dlon/2))^2
  c <- 2 * atan2(sqrt(a), sqrt(1 - a))
  R <- 6378.145
  d <- R * c
  return(d)
}

earth.dist(long1 = lon1, lat1 = lat1, long2 = lon2, lat2 = lat2)
#>   [1] 13.837332 14.373673 23.224936  6.789749  9.954448 16.938167 15.069652
#>   [8] 19.678676 18.369825  3.021305 25.528148 16.103011 14.784745  9.498910
#>  [15] 23.943535  6.525000 22.679474 22.204664  8.378121 11.964816  7.258522
#>  [22] 21.956911 24.884617  2.201575 21.736553 21.048857 15.324981  3.426994
#>  [29]  7.598829 14.227034  1.991978 21.576000 31.370811 17.647770  9.277672
#>  [36] 15.239430 26.598555  6.857203 19.721430 19.404815  1.476280  9.153777
#>  [43] 11.854953  6.996240 20.633853 16.666679  8.282658 23.476963  4.829510
#>  [50]  3.778609 11.296170  1.866245  4.628343  9.530168  3.172872 14.298738
#>  [57]  4.280015 17.448644 24.119368  3.727822 12.217241 12.935120  1.810954
#>  [64] 14.144282  9.464427 10.475816 18.143402  7.371921  5.436596 13.370019
#>  [71] 16.680179 13.985597 36.824188  7.174782 20.542516  5.369208 12.827938
#>  [78] 23.569036 15.200869  6.636130  5.642780 16.832165 23.475004  4.675733
#>  [85]  9.756683  3.955134 12.939690  8.233869 14.160761 16.641892 37.795097
#>  [92]  2.197925  9.036274  7.515637  5.184784 25.172658 13.130789 10.478944
#>  [99]  6.337568  6.131714

df %>% 
  map2(lon1, lat1, earth.dist(.x, .y, lon2, lat2))
#> Error in df %>% map2(lon1, lat1, earth.dist(.x, .y, lon2, lat2)): could not find function "%>%"
Created on 2018-11-09 by the reprex package (v0.2.0).

1 个答案:

答案 0 :(得分:0)

在这种情况下,我们可以使用pmap而不是map2,因为map2仅使用两个参数,而函数具有四个参数(但请确保列名的命名与函数的参数)

library(tidyverse)
df %>% 
  set_names(c('lat1', 'long1', 'lat2', 'long2')) %>% 
  pmap_dbl(earth.dist) %>%
  bind_cols(df, newCol = .)
# A tibble: 100 x 5
#    lat1  lon1  lat2  lon2 newCol
#   <dbl> <dbl> <dbl> <dbl>  <dbl>
# 1  70.0  44.8  70.1  44.9  10.2 
# 2  70.0  45.0  70.1  44.9  12.6 
# 3  69.9  45.1  70.1  44.9  20.3 
# 4  70.2  45.0  70.1  44.9   6.90
# 5  70.0  45.0  70.1  44.9   8.51
# 6  69.8  44.9  70.1  44.9  35.6 
# 7  70.1  44.9  70.1  44.9   6.05
# 8  70.0  44.9  70.1  44.9  19.1 
# 9  69.9  45.0  70.1  44.9  25.7 
#10  70.0  44.9  70.1  44.9  16.2 
# ... with 90 more rows

在OP的帖子中,“ lon1”和“ lat1”作为map2的参数,而将逐元素循环,而“ lon2”和“ lat2”是整个列值,从而在length