mercredi 31 mai 2017

How to exactly unselect xy % of grid cells?

From a 10*10 raster I want to unselect for example 90 percent, that is, 10 percent remain visible. To do this I adapted this code, see below. But there is some variation in the resulting pixels (more then 10 or less then 10 pixels remain). Is there a possibility to set precision of random selection?

r<- raster(ncol=10, nrow=10, xmn=0, ymn=0, xmx=10, ymx=10)#create raster
values(r)<- 1:ncell(1) #asigne 1 to each raster cell
#plot(r, col='black') #plot raster

r[runif(10*10) >= 0.15] <- NA # Randomly *unselect* XY% of the data

par(pty="s", mar=c(1,1,1,1))
plot(r, col='black', legend=FALSE, axes=F) #plot raster
box(lty=1, col="black", lwd=5)




Aucun commentaire:

Enregistrer un commentaire