Created
June 22, 2019 00:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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