英文:
How to overlay more than 2 files in r?
问题
我们可以计算两个堆栈对应层的均值,如下所示。
库(raster)
s1 <- stack(system.file("external/rlogo.grd", package="raster"))
s2 <- sqrt(s1)
m4 <- overlay(s1, s2, fun=function(x) mean(x, na.rm=TRUE))
我有一个文件夹里有几个文件:
data<- list.files ("C:data\\", ".nc", full.names = TRUE)
for (i in 1:length(data)){
nn=stack(dff[i])
res=nn[[1:8]]
...............}
如何对所有的res应用overlay函数
英文:
We can compute the mean of corresponding layers from 2 stacks like this.
library(raster)
s1 <- stack(system.file("external/rlogo.grd", package="raster"))
s2 <- sqrt(s1)
m4 <- overlay(s1, s2, fun=function(x) mean(x, na.rm=TRUE))
I have several files in a folder:
data<- list.files ("C:data\\", ".nc", full.names = TRUE)
for (i in 1:length(data)){
nn=stack(dff[i])
res=nn[[1:8]]
...............}
How can I apply the overlay function to all res
答案1
得分: 0
以下是翻译好的部分:
使用“terra”(替代“raster”)更容易完成此操作,
示例数据
library(terra)
s1 <- rast(system.file("ex/logo.tif", package="terra"))
s2 <- sqrt(s1)
使用两个SpatRasters获取“parallel”均值的解决方案:
mean(s1, s2)
#class : SpatRaster
#dimensions : 77, 101, 3 (nrow, ncol, nlyr)
#resolution : 1, 1 (x, y)
#extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax)
#coord. ref. : Cartesian (Meter)
#source(s) : memory
#names : red, green, blue
#min values : 0.0000, 0.0000, 0.0000
#max values : 135.4844, 135.4844, 135.4844
或者像这样
app(sds(s1, s2), mean)
如果你有多个文件(SpatRasters),你可以这样做:
f <- system.file("ex/logo.tif", package="terra")
ff <- rep(f, 5)
s <- sds(ff)
app(s, mean)
# 或者
x <- lapply(ff, rast)
do.call(mean, x)
# 或者
x <- lapply(ff, rast)
app(sds(x), mean)
在进行计算之前对每个SpatRaster进行子集化,你可以这样做
x <- lapply(ff, \(f) rast(f)[[1:2]])
do.call(mean, x)
或者(但目前存在问题)
s <- sds(ff)
s <- s[, 1:2,drop=FALSE]
app(s, mean)
当然,你也可以使用 [[1:2]]
对输出进行子集化。
请注意,要获取所有层的均值,你可以这样做
mean(c(s1, s2))
# 或者
app(c(s1, s2), mean)
英文:
It is easier to do this with "terra" (the replacement of "raster"),
Example data
library(terra)
s1 <- rast(system.file("ex/logo.tif", package="terra"))
s2 <- sqrt(s1)
Solutions to get the "parallel" mean with two SpatRasters:
mean(s1, s2)
#class : SpatRaster
#dimensions : 77, 101, 3 (nrow, ncol, nlyr)
#resolution : 1, 1 (x, y)
#extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax)
#coord. ref. : Cartesian (Meter)
#source(s) : memory
#names : red, green, blue
#min values : 0.0000, 0.0000, 0.0000
#max values : 135.4844, 135.4844, 135.4844
Or like this
app(sds(s1, s2), mean)
If you have many files (SpatRasters) you can do:
f <- system.file("ex/logo.tif", package="terra")
ff <- rep(f, 5)
s <- sds(ff)
app(s, mean)
# or
x <- lapply(ff, rast)
do.call(mean, x)
# or
x <- lapply(ff, rast)
app(sds(x), mean)
To subset each SpatRaster before the computations, you can do
x <- lapply(ff, \(f) rast(f)[[1:2]])
do.call(mean, x)
Or (but this is currently broken)
s <- sds(ff)
s <- s[, 1:2,drop=FALSE]
app(s, mean)
You can, of course, also subset the output with [[1:2]]
Note that to get the mean of all layers you could do this instead
mean(c(s1, s2))
# or
app(c(s1, s2), mean)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论