
fun=runif
). While there are more direct ways of achieving this for small objects (see examples) for which a vector with all values can be created in memory, the init
function will also work for Raster* objects with many cells.init(x, fun, filename="", ...)
fun=runif
. You can also supply one of the following characwriteRaster
r <- raster(ncols=36, nrows=18)
x <- init(r, fun='cell')
y <- init(r, fun=runif)
# there are different ways to set all values to 1
# for large rasters:
set1f <- function(x){rep(1, x)}
z1 <- init(r, fun=set1f, filename='test.grd', overwrite=TRUE)
# This is equivalent to (but not memory safe):
z2 <- setValues(r, rep(1, ncell(r)))
# or
r[] <- rep(1, ncell(r))
# or
r[] <- 1
Run the code above in your browser using DataLab