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.

0

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

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!
Maybe you can help with the second part of the question as well? <stackoverflow.com/questions/52388444/…>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.