sampleRast: Sample random points from a raster with/out replacement

View source: R/sampleRast.r

sampleRastR Documentation

Sample random points from a raster with/out replacement

Description

This function returns coordinates randomly located on a raster where cells can be sampled more than once if desired (sampled with replacement) and where the probability of selection is proportionate to the cell value (plus maybe cell area–both if desired).

Usage

sampleRast(x, n, adjArea = TRUE, replace = TRUE, prob = TRUE)

Arguments

x

Raster* or SpatRaster object.

n

Positive integer. Number of points to draw.

adjArea

Logical. If TRUE then adjust probabilities so sampling accounts for cell area.

replace

Logical. If TRUE then sample with replacement.

prob

Logical. If TRUE then sample cells with probabilities proportional to cell values. If adjArea is also TRUE then probabilities are drawn proportional to the product of cell area * the value of the cell.

Value

2-column matrix with longitude and latitude of random points. Points will be located at cell centers.

See Also

randomPoints, sampleRastStrat

Examples

library(raster)
r <- raster()
nc <- ncell(r)
r[] <- 1:nc
rands1 <- sampleRast(r, 10000)
rands2 <- sampleRast(r, 10000, adjArea=FALSE)
rands3 <- sampleRast(r, 10000, prob=FALSE)
rands4 <- sampleRast(r, 10000, adjArea=FALSE, prob=FALSE)
par(mfrow=c(2, 2))
plot(r, main='adjArea = TRUE & prob = TRUE')
points(rands1, pch='.')
plot(r, main='adjArea = FALSE & prob = TRUE')
points(rands2, pch='.')
plot(r, main='adjArea = TRUE & prob = FALSE')
points(rands3, pch='.')
plot(r, main='adjArea = FALSE & prob = FALSE')
points(rands4, pch='.')

adamlilith/enmSdm documentation built on Jan. 6, 2023, 11 a.m.