Description Usage Arguments Value Author(s) References Examples
View source: R/random_sample.r
This function draws a spatially random sample from a discrete set of units located over some defined geographical region or generate completely spatially random points within a polygon.
1 |
obj |
a |
poly |
'optional' a |
type |
random sampling, a choice of either |
size |
a non-negative integer giving the total number of locations to be sampled. |
plotit |
'logical' specifying if graphical output is required. Default is |
a sf
or sp
object of dimension n by p=dim(obj)[2]
containing the final sampled locations and any associated values, if sampling from a "discrete"
set of points. A matrix of n by 2 containing sampled locations, if sampling from a "continuum"
.
Michael G. Chipeta mchipeta@mlw.mw
Peter J. Diggle p.diggle@lancaster.ac.uk
Rowlingson, B. and Diggle, P. 1993 Splancs: spatial point pattern analysis code in S-Plus. Computers and Geosciences, 19, 627-655
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | # 1. Sampling from a discrete set of points.
library("dplyr")
x <- 0.015+0.03*(1:33)
xall <- rep(x,33)
yall <- c(t(matrix(xall,33,33)))
xy <- cbind(xall,yall)+matrix(-0.0075+0.015*runif(33*33*2),33*33,2)
colnames(xy) <- c('X','Y')
# Convert to SF
xy <- xy %>%
as.data.frame %>%
sf::st_as_sf(coords = c(1,2))
xy <- sf::st_as_sf(xy, coords = c('X', 'Y'))
# Sampling from a discrete set.
set.seed(15892)
xy.sample <- random.sample(obj = xy, size = 100, type = "discrete", plotit = TRUE)
# Sampling from a continuum.
library("geoR")
data("parana")
poly <- parana$borders
poly <- matrix(c(poly[,1],poly[,2]),dim(poly)[1],2,byrow=FALSE)
# Convert matrix to polygon
poly <- st_sf(st_sfc(st_polygon(list(as.matrix(poly)))))
set.seed(15892)
xy.sample <- random.sample(poly = poly,size = 100, type = "continuum", plotit = TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.