# [R-sig-Geo] Calculate percent cover in a raster

Thiago V. dos Santos thi_veloso at yahoo.com.br
Wed Feb 17 04:44:31 CET 2016

```Hi Ben,

Thanks for your suggestion - it works great.

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

> On Feb 16, 2016, at 8:51 PM, Thiago V. dos Santos <thi_veloso at yahoo.com.br> wrote:
>
> Hi all,
>
> I have a raster at 0.5 degree resolution with monthly totals of rainfall, which range from 50 mm to 600 mm:
>
> 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=50, max=600), digits=0)
>
>
> How can I calculate the percentage area of the raster covered with some rainfall ranges?
>
> Let's say for example 0 to 100, 101 to 200, 201 to 300, 301 to 400, 401 to 500 and finally 501 to 600 mm?
> -- Thiago V. dos Santos
>
> PhD student
> Land and Atmospheric Science
> University of Minnesota
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

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

```