Skip to content
Prev 24020 / 29559 Next

Calculate percent cover in a raster

Hi Ben,

Thanks for your suggestion - it works great.

What I ended up using was this:

require(raster)

## scratch a raster and fill some random values
r <- raster(nrows=22, ncols=20, xmn=-58, xmx=-48, ymn=-33, ymx=-22)
r[] <- round(runif(22 * 20, min=0, max=600), digits=0)

# Count raster cells (excluding NA's) and set intervals
ncells <- sum(!is.na(r[]))
intervals <- seq(0, 600, 100)

# Count frequencies and calculate percentage
t <- table(cut(as.vector(r), intervals, include.lowest=TRUE))
t <- t/ncells * 100

# Take a look at it
df <- data.frame(t)
print(df)


 Greetings,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota
On Tuesday, February 16, 2016 9:14 PM, Ben Tupper <btupper at bigelow.org> wrote:
Hi,

I think you can use raster::reclassify() or raster::cut() to bin your rainfall.

intervals <- seq(from = 0, to = 600, by = 100)
ix <- cut(r, breaks = intervals)
tx <- table(as.vector(ix))
tx

  1   2   3   4   5   6 
46  64  85  76 100  69 

tx/sum(tx)*100

       1        2        3        4        5        6 
10.45455 14.54545 19.31818 17.27273 22.72727 15.68182 

Is that what you are looking for?


Cheers,
Ben
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org