spatialsample A hand-drawn map with orange roads, blue rivers, green trees, and brown mountains on a beige background

R-CMD-check CRAN status Codecov test coverage Lifecycle: experimental


The goal of spatialsample is to provide functions and classes for spatial resampling to use with rsample, including:

Like rsample, spatialsample provides building blocks for creating and analyzing resamples of a spatial data set but does not include code for modeling or computing statistics. The resampled data sets created by spatialsample are efficient and do not have much memory overhead.


You can install the released version of spatialsample from CRAN with:


And the development version from GitHub with:

# install.packages("pak")


The most straightforward spatial resampling strategy is spatial_clustering_cv(), which uses k-means clustering to identify cross-validation folds:


folds <- spatial_clustering_cv(boston_canopy, v = 5)

#> #  5-fold spatial cross-validation 
#> # A tibble: 5 × 2
#>   splits            id   
#>   <list>            <chr>
#> 1 <split [604/78]>  Fold1
#> 2 <split [595/87]>  Fold2
#> 3 <split [524/158]> Fold3
#> 4 <split [490/192]> Fold4
#> 5 <split [515/167]> Fold5

In this example, the boston_canopy data on tree cover in Boston, MA is resampled with v = 5; notice that the resulting partitions do not contain an equal number of observations.

In addition to resampling algorithms, spatialsample provides methods to visualize resamples using ggplot2 through the autoplot() function:


We can use the same function to visualize each fold separately:


walk(folds$splits, function(x) print(autoplot(x)))

So far, we’ve only scratched the surface of the functionality spatialsample provides. For more information, check out the Getting Started documentation!


This project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.