mercredi 8 mars 2023

How can I extract random samples of spatial points using "sample function" and repeat the process with "for loops" in R

I am working in RStudio for Mac OS and I am struggling with a code. I have a SpatialPointsDataFrame consisting of 128 points with three collumns (ID, Easat, North). I would like to extract a random sample of 10% of these sites (i.e. n = 115). In addition, I would like to repeat this process 10 times, i.e. create 10 iterations of the extraction of random samples. My aim is to obtain 10 separate groups that contain 115 random points extracted from the original 128 points.

I have try several codes unsuccessfully, including for loops, function and lapply. But I either don't get what I am looking for or get different type of erros. The simplest code that has got me closer is this:

nsites <- sites # defining my database
nsim <- 10 # setting the number of simulations
nsamp <- 115 # defining the number of points I want to extract
s90 <- nsites[sample(nrow(nsites), size = nsamp, replace = FALSE, prob = NULL), ]
for(i in 1:nsim) {
        s90r[nsim] <- list(s90) # create a list to store the results
        print(s90r)
}

This result in 10 groups of points, but when I plot each group I realized that they are the same 115 points, and not 10 groups of -different- random 115 points.

Thank you very for any suggestion!

Best,

Eduardo




Aucun commentaire:

Enregistrer un commentaire