1

I want to write a df with the freq of 150 raster objects.

I know I can read individual raster objects with

raster_x <- raster::raster()

I can further get the freq with

raster_freq_y <- raster::freq(raster_x)

After that I can bind the freq outputs of multiple raster objects to a df with

cbind.data.frame(raster_freq_x, raster_freq_y) 

What I dont know is how to do this for 150 raster objects in one go?

Should I use a loop? If so what kind of loop would make sense?

Any help is appreciated.

loening
  • 13
  • 3

1 Answers1

1

If the RasterLayer objects have the same extent and resolution, you can combine them in a RasterStack. The below example is from ?freq

Example data:

library(raster)
r <- raster(nrow=18, ncol=36)
r[] <- runif(ncell(r))
r[1:5] <- NA
s <- stack(r, r*2, r*3)

Solution:

freq(s, merge=TRUE)

If the RasterLayer objects do not have the same extent and resolution, you can keep them together in a list and use lapply

ss <- list(r, r*2, r*3)
lapply(ss, freq)
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • They dont have the same extent but the same resolution. Creating a list with ```spatial.tools::list.raster.files()``` and then calling ```lapply(ls$raster,freq)``` did the trick. I really have to learn more about lapply.Thank you so much! – loening Sep 18 '18 at 13:18
  • Maybe you can help with the second part of the question as well? – loening Sep 18 '18 at 14:07