0

I would like to take a systematic sample of cells from a raster layer. The raster that I am using to select the sample has been divided into 15 sections. I would like to sample 2 cells from each section, but I would like the relative position of the sampled cells to be the same in each section.

eg. If the cell at the upper right of the first section is selected, I would like to sample the upper right cell of the other 14 sections. But I would like the 'starting place' to be random.

This is what my raster looks like, where the cell value represents what section it belongs to

r<-raster(ncol=36, nrow=35)
r[1:7,1:12]<-1
r[1:7,13:24]<-2
r[1:7,25:36]<-3
r[8:14,1:12]<-4
r[8:14,13:24]<-5
r[8:14,25:36]<-6
r[15:21,1:12]<-7
r[15:21,13:24]<-8
r[15:21,25:36]<-9
r[22:28,1:12]<-10
r[22:28,13:24]<-11
r[22:28,25:36]<-12
r[29:35,1:12]<-13
r[29:35,13:24]<-14
r[29:35,25:36]<-15
plot(r)

enter image description here

How might I do this?

Splash1199
  • 379
  • 3
  • 14
  • 1
    There seems to be something amiss. You declare your raster with 35 columns and 33 rows, but then when you populate the raster, you are assigning values to rows up to 35 and columns up to 36. – G5W Mar 30 '17 at 15:41
  • sorry about that, I corrected the error – Splash1199 Mar 30 '17 at 15:47

1 Answers1

0

This is from another very similar question I answered, but there the user wanted a randomly placed sample from a 3x3 grid within the larger raster.

library(raster)
ras <- raster(nrows = 3, ncols = 3)
v <- c(1,2,NA,4,NA,NA,7,8,9)

ras[] <- v
plot(ras)
res(ras)

ras_fine <- raster(nrows = 9, ncols = 9) #finer resolution raster
ras<-resample(ras,ras_fine,method='ngb')
plot(ras)

samp_strat.rand<- sampleStratified(ras, 1, xy = TRUE, sp=TRUE, na.rm = TRUE)

plot(ras)
points(samp_strat.rand)

With a little tweaking:

library(raster)
r<-raster(ncol=36, nrow=35)
r[1:7,1:12]<-1
r[1:7,13:24]<-2
r[1:7,25:36]<-3
r[8:14,1:12]<-4
r[8:14,13:24]<-5
r[8:14,25:36]<-6
r[15:21,1:12]<-7
r[15:21,13:24]<-8
r[15:21,25:36]<-9
r[22:28,1:12]<-10
r[22:28,13:24]<-11
r[22:28,25:36]<-12
r[29:35,1:12]<-13
r[29:35,13:24]<-14
r[29:35,25:36]<-15
r
plot(r)
res(r)

#ras_fine <- raster(nrows = 9, ncols = 9) #finer resolution raster
#ras<-resample(ras,ras_fine,method='ngb')
#plot(ras)

samp_strat.rand<- sampleStratified(r, 2, xy = TRUE, sp=TRUE, na.rm = TRUE)

plot(r)
points(samp_strat.rand)

So we get two random sample points per cell.. and with some minor adjustment...

samp_strat.reg<- sampleRegular(r, 36, xy = TRUE, sp=TRUE, na.rm = TRUE)

samp_strat.reg.off<-data.frame(samp_strat.reg)

coordinates(samp_strat.reg.off) <- cbind(samp_strat.reg$x+runif(1,min=0,max=res(r)[1]),samp_strat.reg$y+runif(1,min=0,max=res(r)[2]))
samp_strat.off<-samp_strat.reg.data
plot(r)
points(samp_strat.off,col="red")
points(samp_strat.reg,col="blue")

Now we have a randomly located point within the confines of a single cell, the same random point each time. I think this was what you were looking for.

SeldomSeenSlim
  • 811
  • 9
  • 24