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

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

How to overlay more than 2 files in r?

问题

我们可以计算两个堆栈对应层的均值,如下所示。

  1. 库(raster
  2. s1 <- stack(system.file("external/rlogo.grd", package="raster"))
  3. s2 <- sqrt(s1)
  4. m4 <- overlay(s1, s2, fun=function(x) mean(x, na.rm=TRUE))

我有一个文件夹里有几个文件:

  1. data<- list.files ("C:data\\", ".nc", full.names = TRUE)
  2. for (i in 1:length(data)){
  3. nn=stack(dff[i])
  4. res=nn[[1:8]]
  5. ...............}

如何对所有的res应用overlay函数

英文:

We can compute the mean of corresponding layers from 2 stacks like this.

  1. library(raster)
  2. s1 &lt;- stack(system.file(&quot;external/rlogo.grd&quot;, package=&quot;raster&quot;))
  3. s2 &lt;- sqrt(s1)
  4. m4 &lt;- overlay(s1, s2, fun=function(x) mean(x, na.rm=TRUE))

I have several files in a folder:

  1. data&lt;- list.files (&quot;C:data\\&quot;, &quot;.nc&quot;, full.names = TRUE)
  2. for (i in 1:length(data)){
  3. nn=stack(dff[i])
  4. res=nn[[1:8]]
  5. ...............}

How can I apply the overlay function to all res

答案1

得分: 0

以下是翻译好的部分:

使用“terra”(替代“raster”)更容易完成此操作,

示例数据

  1. library(terra)
  2. s1 <- rast(system.file("ex/logo.tif", package="terra"))
  3. s2 <- sqrt(s1)

使用两个SpatRasters获取“parallel”均值的解决方案:

  1. mean(s1, s2)
  2. #class : SpatRaster
  3. #dimensions : 77, 101, 3 (nrow, ncol, nlyr)
  4. #resolution : 1, 1 (x, y)
  5. #extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax)
  6. #coord. ref. : Cartesian (Meter)
  7. #source(s) : memory
  8. #names : red, green, blue
  9. #min values : 0.0000, 0.0000, 0.0000
  10. #max values : 135.4844, 135.4844, 135.4844

或者像这样

  1. app(sds(s1, s2), mean)

如果你有多个文件(SpatRasters),你可以这样做:

  1. f <- system.file("ex/logo.tif", package="terra")
  2. ff <- rep(f, 5)
  3. s <- sds(ff)
  4. app(s, mean)
  5. # 或者
  6. x <- lapply(ff, rast)
  7. do.call(mean, x)
  8. # 或者
  9. x <- lapply(ff, rast)
  10. app(sds(x), mean)

在进行计算之前对每个SpatRaster进行子集化,你可以这样做

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

或者(但目前存在问题)

  1. s <- sds(ff)
  2. s <- s[, 1:2,drop=FALSE]
  3. app(s, mean)

当然,你也可以使用 [[1:2]] 对输出进行子集化。

请注意,要获取所有层的均值,你可以这样做

  1. mean(c(s1, s2))
  2. # 或者
  3. app(c(s1, s2), mean)
英文:

It is easier to do this with "terra" (the replacement of "raster"),

Example data

  1. library(terra)
  2. s1 &lt;- rast(system.file(&quot;ex/logo.tif&quot;, package=&quot;terra&quot;))
  3. s2 &lt;- sqrt(s1)

Solutions to get the "parallel" mean with two SpatRasters:

  1. mean(s1, s2)
  2. #class : SpatRaster
  3. #dimensions : 77, 101, 3 (nrow, ncol, nlyr)
  4. #resolution : 1, 1 (x, y)
  5. #extent : 0, 101, 0, 77 (xmin, xmax, ymin, ymax)
  6. #coord. ref. : Cartesian (Meter)
  7. #source(s) : memory
  8. #names : red, green, blue
  9. #min values : 0.0000, 0.0000, 0.0000
  10. #max values : 135.4844, 135.4844, 135.4844

Or like this

  1. app(sds(s1, s2), mean)

If you have many files (SpatRasters) you can do:

  1. f &lt;- system.file(&quot;ex/logo.tif&quot;, package=&quot;terra&quot;)
  2. ff &lt;- rep(f, 5)
  3. s &lt;- sds(ff)
  4. app(s, mean)
  5. # or
  6. x &lt;- lapply(ff, rast)
  7. do.call(mean, x)
  8. # or
  9. x &lt;- lapply(ff, rast)
  10. app(sds(x), mean)

To subset each SpatRaster before the computations, you can do

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

Or (but this is currently broken)

  1. s &lt;- sds(ff)
  2. s &lt;- s[, 1:2,drop=FALSE]
  3. 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

  1. mean(c(s1, s2))
  2. # or
  3. 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:

确定