Reputation: 119
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)
Upvotes: 0
Views: 13
Reputation: 18425
Instead of your runif
line, use
r[sample(ncell(r),ncell(r)*0.9)] <- NA
This picks exactly 90% of the cells at random.
Upvotes: 1