pointsToRaster(raster, xy, values=1, fun=length, background=NA, filename="", ...)
length
, i.e. it returns the number of points in a cell. Typical examples of other functions that can be used are mean, sum, max and function(
NA
or 0
)length
function. I.e. for each cell it computes the length
of the vector of points.
For the sum of the values, use sum
, for a yes/no result, you can use fun=function(x){length(x)>0}
For the number of unique values use fun=function(x){length(unique(x))}
The following additional arguments can be passed, to replace default values for this function
overwrite
Logical. If TRUE
, "filename" will be overwritten if it exists
format
Character. Output file type. See writeRaster
datatype
Character. Output data type. See dataType
progress
Character. "text", "window", or "" (the default, no progress bar)
}linesToRaster
, polygonsToRaster
r <- raster(ncols=36, nrows=18)
n <- 1000
x <- runif(n)* 360 - 180
y <- runif(n)* 180 - 90
xy <- cbind(x, y)
vals <- rep(1, n)
r <- pointsToRaster(r, xy, vals)
# now with an sp SpatialPointsDataFrame object
p <- as.data.frame(cbind(xy, vals))
coordinates(p) <- ~x+y
r <- pointsToRaster(r, p, p@data[,'vals'])
Run the code above in your browser using DataLab