How can I speed up spatial operations in `dplyr::mutate()`?

蹲街弑〆低调 提交于 2019-12-05 08:17:10

you can considerably speed-up this by simply dropping the unnecessary map_lgl call in the pipe:

bm_sf_dplyr_large_fast <- benchmark({
  int_new <- nc_1e4 %>% mutate(INT = st_intersects_any(., nc_wtr))
}, columns = cols, replications = 1)
bm_sf_dplyr_large_fast

# bm_sf_dplyr_large_fast
# elapsed relative
# 1   0.829        1

The huge slow down depends from the fact that mapping over geometry rows is in this case detrimental, because you then do a looped one-to-multi polygon intersection.

Besides the overhead introduced by subsetting, I believe this is much slower than a straight-on multi-to-multi because you are probably mostly losing the "spatial indexing" capabilities of sf objects, which considerably speed-up intersect operations (see http://r-spatial.org/r/2017/06/22/spatial-index.html). (Also note that I substituted transmute' withmutate` - also that was introducing some overhead).

HTH

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!