raster (version 2.1-41)

initialize: Intitialize

Description

Create a new RasterLayer with values reflecting a cell property: v from 'x', 'y', 'col', 'row', or 'cell'. Alternatively, a function can be used. In that case, cell values are initialized without reference to pre-existing values. E.g., initialize with a random number (fun=runif). Either supply an argument to fun, or to v, but not both.

Usage

init(x, fun, v, filename="", ...)

Arguments

x
A Raster* object
fun
The function to be applied. This must be a function that can take the number of cells as a single argument to return a vector of values with a length equal to the number of cells
v
'x', 'y', 'row', 'col', or 'cell'
filename
Output filename
...
Additional arguments as for writeRaster

Value

  • RasterLayer

Examples

Run this code
r <- raster(ncols=36, nrows=18)

x <- init(r, v='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)

# equivalent to
z2 <- setValues(r, rep(1, ncell(r)))
# or  
r[] <- rep(1, ncell(r))
# or  
r[] <- 1

Run the code above in your browser using DataCamp Workspace