Search code examples
rrandomraster

Why are random values making pattern?


I tried to generate array of zeros and ones, convert to raster and plot it. I expect to have random pattern, so why there is apparent pattern in this raster? Have I done some mistake in script?

# creating vector containing "0" and "1" values...
x<-sample(c(0,1), 1000, replace=TRUE)

# ...converting it into array...
x_arr<-array(x, dim=c(100,100))

# ....nest into raster
x_rast<-raster(x_arr)

# ...and making plot
plot(x_rast)

enter image description here


Solution

  • Because 100*100 gives 10000, not 1000, and R will fill up by repeating. Try

    library(raster)
    x<-sample(c(0,1), 10000, replace=TRUE)
    

    The example was nice, but please do not forget to paste it into an virgin R before posting. You had forgotten the library.