Count freq of multiple raster objects in R

故事扮演 提交于 2019-12-11 12:10:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!