I'm working on a sf
points object.
I wrote a code to do some grouping and computing mean distance to the nearest neighbor and count by cities.
When I do all the processus with dplyr
and purrr
, it's working nicely (but it takes some time because my actual data is massive).
So, I just wanted to do some parallel computation with furrr
, but it seems that if the map
function works well on a list of sf objects, this is not the case for the future_map
function.
I end up with the following error, Caused by error in vec_size(): ! x must be a vector, not a <sfc_POINT/sfc> object.
Does anyone know what is wrong here?
# purrr version that's working
library(tidyverse)
library(spatstat)
library(sf)
points_dist <- points_sf %>%
# remove points ID
select(-clenum) %>%
# pivot longer all the flag column (as date)
pivot_longer(-c(geometry, City), names_to = "Date", values_to = "Presence") %>%
# group by date and city code
group_by(Date, City) %>%
# split the df into a named list
{setNames(group_split(., .keep = F),paste(group_keys(.)[[1]],group_keys(.)[[2]], sep = "_"))} %>%
# keep only the points that are here for a given date and city
map(., ~filter( ., Presence == 1)) %>%
# get a df by date and city of points mean distances and count
imap_dfr(.,
~summarise(.,
Ra = mean(nndist(as.ppp(.), k=1),na.rm = T)/1000, # compute mean nearest neighbor distance (km)
Nagence = sum(Presence)), # count points,
.id = "Group") %>%
st_drop_geometry() %>%
drop_units() %>%
separate(., Group, into = c("Date", "City"), sep = "_")
head(points_dist)
Date City Ra Nagence
<chr> <chr> <dbl> <dbl>
1 flag00 418 0.0563 8
2 flag00 430 0.410 12
3 flag00 438 1.00 7
4 flag01 418 0.0563 8
5 flag01 430 0.410 12
6 flag01 438 1.00 7
7 flag02 418 0.0563 8
8 flag02 430 0.410 12
9 flag02 438 1.00 7
10 flag03 418 0.0563 8
Now I want to do the exact same thing but in parallel with future_map
and future_imap_dfr
# not working with furrr
library(tidyverse)
library(furrr)
library(parallel)
library(spatstat)
plan(multisession, workers = detectCores()-2)
points_dist_parr <- points_sf %>%
select(-clenum) %>%
pivot_longer(-c(geometry, City), names_to = "Date", values_to = "Presence") %>%
group_by(Date, City) %>%
{setNames(group_split(., .keep = F),paste(group_keys(.)[[1]],group_keys(.)[[2]], sep = "_"))} %>%
future_map(., ~filter( ., Presence == 1)) %>% # this step doesn't work
future_imap_dfr(.,
~summarise(.,
Ra = mean(nndist(as.ppp(.), k=1),na.rm = T)/1000,
Nagence = sum(Presence)),
.id = "Group") %>%
st_drop_geometry() %>%
drop_units() %>%
separate(., Group, into = c("Date", "City"), sep = "_")
## i get the following error
Error in (function (.x, .f, ..., .progress = FALSE) :
ℹ In index: 1.
ℹ With name: flag00_418.
Caused by error in `vec_size()`:
! `x` must be a vector, not a <sfc_POINT/sfc> object.
Below a sample of my sf points object for reprex.
# a small reprex sf object
points_sf <- structure(list(clenum = c(980587, 98054, 980513, 980520, 980553,
980571, 980599, 12052, 985032, 9850116, 9850168, 9850180, 9850192,
9850255, 9850419, 9850441, 985621, 985634, 985666, 9856179, 9856230,
9856250, 9856267, 9856289, 9856400, 9856587, 9856517, 9856234,
9856455), City = c("438", "438", "438", "438", "438", "438",
"438", "438", "418", "418", "418", "418", "418", "418", "418",
"418", "430", "430", "430", "430", "430", "430", "430", "430",
"430", "430", "430", "430", "430"), flag98 = c(1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1), flag99 = c(1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), flag00 = c(1, 1,
1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1), flag01 = c(1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1), flag02 = c(1,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1), flag03 = c(1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
1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 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
1, 1, 1, 0, 1, 1, 1), flag15 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1), flag16 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
1, 1, 1, 0, 1, 1, 1), flag17 = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1), flag18 = c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1,
1, 1, 1, 0, 1, 1, 1), geometry = structure(list(structure(c(4042673.64358386,
2383567.60640022), class = c("XY", "POINT", "sfg")), structure(c(4042282.31916301,
2390340.82287981), class = c("XY", "POINT", "sfg")), structure(c(4042223.43506352,
2390290.84163531), class = c("XY", "POINT", "sfg")), structure(c(4042135.56145553,
2390338.71430411), class = c("XY", "POINT", "sfg")), structure(c(4041953.40753155,
2390204.54610263), class = c("XY", "POINT", "sfg")), structure(c(4042202.44259419,
2390199.71510174), class = c("XY", "POINT", "sfg")), structure(c(4041953.40753155,
2390204.54610263), class = c("XY", "POINT", "sfg")), structure(c(4048241.63991944,
2392475.82572007), class = c("XY", "POINT", "sfg")), structure(c(3494010.63830182,
2997650.75440965), class = c("XY", "POINT", "sfg")), structure(c(3493956.39946714,
2997554.96475846), class = c("XY", "POINT", "sfg")), structure(c(3494032.15495681,
2997531.65975932), class = c("XY", "POINT", "sfg")), structure(c(3494032.15495681,
2997531.65975932), class = c("XY", "POINT", "sfg")), structure(c(3494032.15495681,
2997531.65975932), class = c("XY", "POINT", "sfg")), structure(c(3494172.20007342,
2997575.87814808), class = c("XY", "POINT", "sfg")), structure(c(3493908.8641747,
2997703.04921459), class = c("XY", "POINT", "sfg")), structure(c(3494032.15495681,
2997531.65975932), class = c("XY", "POINT", "sfg")), structure(c(3342382.633314,
2805756.05766125), class = c("XY", "POINT", "sfg")), structure(c(3342230.65029595,
2805751.41488307), class = c("XY", "POINT", "sfg")), structure(c(3342072.30450955,
2805937.95663458), class = c("XY", "POINT", "sfg")), structure(c(3342158.94953116,
2805824.72334241), class = c("XY", "POINT", "sfg")), structure(c(3342429.98390154,
2804532.36764919), class = c("XY", "POINT", "sfg")), structure(c(3342299.17426621,
2805705.66517774), class = c("XY", "POINT", "sfg")), structure(c(3342140.03080349,
2805835.85122467), class = c("XY", "POINT", "sfg")), structure(c(3342197.25975494,
2805768.14774248), class = c("XY", "POINT", "sfg")), structure(c(3342282.59901775,
2805428.28042927), class = c("XY", "POINT", "sfg")), structure(c(3342157.23216705,
2805788.62580392), class = c("XY", "POINT", "sfg")), structure(c(3339960.60730655,
2807817.52139131), class = c("XY", "POINT", "sfg")), structure(c(3346200.05093248,
2805519.51740192), class = c("XY", "POINT", "sfg")), structure(c(3346061.85639013,
2805317.84414013), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT",
"sfc"), precision = 0, bbox = structure(c(xmin = 3339960.60730655,
ymin = 2383567.60640022, xmax = 4048241.63991944, ymax = 2997703.04921459
), class = "bbox"), crs = structure(list(input = "EPSG:3035",
wkt = "PROJCRS[\"ETRS89-extended / LAEA Europe\",\n BASEGEOGCRS[\"ETRS89\",\n ENSEMBLE[\"European Terrestrial Reference System 1989 ensemble\",\n MEMBER[\"European Terrestrial Reference Frame 1989\"],\n MEMBER[\"European Terrestrial Reference Frame 1990\"],\n MEMBER[\"European Terrestrial Reference Frame 1991\"],\n MEMBER[\"European Terrestrial Reference Frame 1992\"],\n MEMBER[\"European Terrestrial Reference Frame 1993\"],\n MEMBER[\"European Terrestrial Reference Frame 1994\"],\n MEMBER[\"European Terrestrial Reference Frame 1996\"],\n MEMBER[\"European Terrestrial Reference Frame 1997\"],\n MEMBER[\"European Terrestrial Reference Frame 2000\"],\n MEMBER[\"European Terrestrial Reference Frame 2005\"],\n MEMBER[\"European Terrestrial Reference Frame 2014\"],\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]],\n ENSEMBLEACCURACY[0.1]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4258]],\n CONVERSION[\"Europe Equal Area 2001\",\n METHOD[\"Lambert Azimuthal Equal Area\",\n ID[\"EPSG\",9820]],\n PARAMETER[\"Latitude of natural origin\",52,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8801]],\n PARAMETER[\"Longitude of natural origin\",10,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8802]],\n PARAMETER[\"False easting\",4321000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8806]],\n PARAMETER[\"False northing\",3210000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8807]]],\n CS[Cartesian,2],\n AXIS[\"northing (Y)\",north,\n ORDER[1],\n LENGTHUNIT[\"metre\",1]],\n AXIS[\"easting (X)\",east,\n ORDER[2],\n LENGTHUNIT[\"metre\",1]],\n USAGE[\n SCOPE[\"Statistical analysis.\"],\n AREA[\"Europe - European Union (EU) countries and candidates. Europe - onshore and offshore: Albania; Andorra; Austria; Belgium; Bosnia and Herzegovina; Bulgaria; Croatia; Cyprus; Czechia; Denmark; Estonia; Faroe Islands; Finland; France; Germany; Gibraltar; Greece; Hungary; Iceland; Ireland; Italy; Kosovo; Latvia; Liechtenstein; Lithuania; Luxembourg; Malta; Monaco; Montenegro; Netherlands; North Macedonia; Norway including Svalbard and Jan Mayen; Poland; Portugal including Madeira and Azores; Romania; San Marino; Serbia; Slovakia; Slovenia; Spain including Canary Islands; Sweden; Switzerland; Turkey; United Kingdom (UK) including Channel Islands and Isle of Man; Vatican City State.\"],\n BBOX[24.6,-35.58,84.73,44.83]],\n ID[\"EPSG\",3035]]"), class = "crs"), n_empty = 0L)), row.names = c(NA,
29L), sf_column = "geometry", agr = structure(c(clenum = NA_integer_,
City = NA_integer_, flag98 = NA_integer_, flag99 = NA_integer_,
flag00 = NA_integer_, flag01 = NA_integer_, flag02 = NA_integer_,
flag03 = NA_integer_, flag04 = NA_integer_, flag05 = NA_integer_,
flag06 = NA_integer_, flag07 = NA_integer_, flag08 = NA_integer_,
flag09 = NA_integer_, flag10 = NA_integer_, flag11 = NA_integer_,
flag12 = NA_integer_, flag13 = NA_integer_, flag14 = NA_integer_,
flag15 = NA_integer_, flag16 = NA_integer_, flag17 = NA_integer_,
flag18 = NA_integer_), levels = c("constant", "aggregate", "identity"
), class = "factor"), class = c("sf", "tbl_df", "tbl", "data.frame"
))