Search code examples
rrandomrastersample

how to randomly show a high number of points in an area in R


I am trying to collect random sample points from an area (polygons) to later cross-check information from these points in a raster. However, sometimes I need a high number of points. When I try to run my code, R returns an error.

I used this code, which worked great for small numbers.

#create a set of random point samples

library(raster)
library(sp)
r_sample <- spsample(my_shp, n= 212183, type='random') 

#cross and extract raster information based on previously generated points
 
extrc <- raster::extract(my_raster, r_sample, small=T, na.rm=F) #extrair as quantidades de pixels 

error

> r_sample <- spsample(controle, n= 212183 , type='random')
> Error: Unable to allocate vector of size 1.9 Gb

Solution

  • If it works for small numbers you can run it a couple of times

    x <- lapply(1:10, function(i) spsample(controle, n= 21218 , type='random'))
    

    And combine them like this:

    y <- do.call(rbind, x)