问题
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.
回答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)
来源:https://stackoverflow.com/questions/52365579/count-freq-of-multiple-raster-objects-in-r