sf
sf copied to clipboard
sample_n.sf can't handle grouped samples when sample size refers to another column
Consider the following pattern whereby I indicate an ideal sample size per group, but if the group size is not large enough, it defaults to the group size:
suppressPackageStartupMessages(library(dplyr))
#> Warning: package 'dplyr' was built under R version 4.0.2
new_iris <- sample_n(iris, 30) %>%
mutate(Species = "New Species") %>%
bind_rows(iris)
new_iris %>%
add_count(Species, name = "obs") %>%
group_by(Species) %>%
mutate(sample_size = ifelse(obs < 40, obs, 40)) %>%
sample_n(sample_size, replace = FALSE) %>%
count(Species)
#> # A tibble: 4 x 2
#> # Groups: Species [4]
#> Species n
#> <chr> <int>
#> 1 New Species 30
#> 2 setosa 40
#> 3 versicolor 40
#> 4 virginica 40
Created on 2020-12-06 by the reprex package (v0.3.0)
This same pattern fails with objects of class 'sf':
library(spData)
library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.2, PROJ 5.2.0
suppressPackageStartupMessages(library(dplyr))
world %>%
add_count(continent, name = "obs") %>%
group_by(continent) %>%
mutate(sample_size = ifelse(obs < 10, obs, 10)) %>%
sample_n(sample_size, replace = FALSE)
#> Error in ~size: object 'sample_size' not found
Created on 2020-12-06 by the reprex package (v0.3.0)
Is this a bug or is there another method for obtaining these results?