如何在R中叠加超过2个文件?

huangapple go评论77阅读模式
英文:

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 &lt;- stack(system.file(&quot;external/rlogo.grd&quot;, package=&quot;raster&quot;)) 
s2 &lt;- sqrt(s1)
m4 &lt;- overlay(s1, s2, fun=function(x) mean(x, na.rm=TRUE))

I have several files in a folder:

 data&lt;- list.files (&quot;C:data\\&quot;, &quot;.nc&quot;, 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 &lt;- rast(system.file(&quot;ex/logo.tif&quot;, package=&quot;terra&quot;))   
s2 &lt;- 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 &lt;- system.file(&quot;ex/logo.tif&quot;, package=&quot;terra&quot;)
ff &lt;- rep(f, 5)

s &lt;- sds(ff)
app(s, mean)

# or 
x &lt;- lapply(ff, rast)
do.call(mean, x)

# or
x &lt;- lapply(ff, rast)
app(sds(x), mean)

To subset each SpatRaster before the computations, you can do

x &lt;- lapply(ff, \(f) rast(f)[[1:2]])
do.call(mean, x)

Or (but this is currently broken)

s &lt;- sds(ff)
s &lt;- 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)

huangapple
  • 本文由 发表于 2023年4月16日 23:38:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76028700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定