Skip to content

Instantly share code, notes, and snippets.

@obrl-soil
Created June 22, 2019 00:07
Show Gist options
  • Save obrl-soil/8abab92e311de6edbeeef01d34c4575c to your computer and use it in GitHub Desktop.
Save obrl-soil/8abab92e311de6edbeeef01d34c4575c to your computer and use it in GitHub Desktop.
R - Grow data patches in a raster by one cell at a time
# after https://twitter.com/ColinJCarlson/status/1142202184324530176
# kind of riffs of the game of life demo in ?raster::focal
grow_by_one <- function(x = NULL) {
focal(x, w = matrix(1, ncol = 3, nrow = 3),
fun = function(win) {
if(!is.na(win[5])) {
return(win[5])
}
if(any(!is.na(win[c(2,4,6,8)]))) {
mean(win, na.rm = TRUE)
} else {
NA
}
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment