nathaneastwood / poorman

A poor man's dependency free grammar of data manipulation
https://nathaneastwood.github.io/poorman/
Other
338 stars 15 forks source link

Dissolve sf polygons based on group_by() #53

Open fdetsch opened 3 years ago

fdetsch commented 3 years ago

Not sure whether this goes way beyond the scope of the poorman, but would basic dissolve functionality for sf objects - as offered by dplyr - be something you might want to think about for a future release? A quick example of what I mean can be found here.

Here's the condensed dplyr approach from above link:

library(raster)
library(sf)
library(dplyr)

## regions
regions = getData(
  country = "GBR"
  , level = 2
  , path = tmpDir()
) %>% 
  st_as_sf() %>% 
  filter(NAME_1 == "England")

plot(regions['NAME_2'])

## dissolve
england = regions %>% 
  group_by(NAME_1) %>% 
  summarise()

plot(england)

The poorman currently fails to carry out the group_by() operation with an

"Error in order(y) : unimplemented type 'list' in 'orderVector1'"

nathaneastwood commented 3 years ago

I've looked into this a little bit and I fixed the problems with group_by() (locally, not yet pushed). However the way that this works for {dplyr} is they provide a way for package authors to create methods for their S3 generics (see here). Therefore when you run regions %>% group_by(NAME_1), the output class is [1] "sf" "grouped_df" "tbl_df" "tbl" "data.frame". Hence when you call summarise() on this output, it dispatches to summarise.sf() which uses some of {dplyr}'s functionality so unfortunately, without buy-in from package authors, you will never really be able to use {sf} with {poorman} unless I were to implement these methods myself.