random - simple way to randomly subset a raster in R -
i work in r. have raster (raster_entrop). want automatically generate random subset of raster. issue extent not rectangular , want subsets completly enclosed in extent. solution check if there not "nan" (corresponding pixel outside extent of raster_entrop) in coordinate of "subextent" randomly chosen inside extent of original raster.
ext<-extent(raster_entrop) repeat{ rnd.x<-runif(1,ext@xmin, ext@xmax) rnd.y<-runif(1,ext@ymin, ext@ymax) #test if sub extent within "big extent" test<-is.na(extract(raster_entrop, cbind(rnd.x,rnd.y)))+ is.na(extract(raster_entrop, cbind(rnd.x,rnd.y+0.02)))+ is.na(extract(raster_entrop, cbind(rnd.x+(0.02),rnd.y)))+ is.na(extract(raster_entrop, cbind(rnd.x+(0.02),rnd.y+0.02))) if (test==0){ break } } ext_sub<-extent(c(rnd.x, rnd.x+0.02,rnd.y,rnd.y+0.02)) raster_entrop_sub <- crop(raster_entrop, ext_sub)
my question: there simpler solution not use loop ?