Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 与多个光栅相关的嵌套条件语句_R_If Statement_Nested_Conditional Statements_Raster - Fatal编程技术网

R 与多个光栅相关的嵌套条件语句

R 与多个光栅相关的嵌套条件语句,r,if-statement,nested,conditional-statements,raster,R,If Statement,Nested,Conditional Statements,Raster,假设我有四个光栅文件:x、y、w和z,我想根据它们之间的关系(即网格单元的层次顺序)使用多个嵌套条件创建一个新的光栅文件,如下所示: x <- raster(nrows=100, ncols=100) x[] <- runif(ncell(x), min = 0, max = 100) y <- raster(nrows=100, ncols=100) y[] <- runif(ncell(y), min = 0, max = 100) w <- raste

假设我有四个光栅文件:x、y、w和z,我想根据它们之间的关系(即网格单元的层次顺序)使用多个嵌套条件创建一个新的光栅文件,如下所示:

x <- raster(nrows=100, ncols=100)
  x[] <- runif(ncell(x), min = 0, max = 100)
y <- raster(nrows=100, ncols=100)
  y[] <- runif(ncell(y), min = 0, max = 100)
w <- raster(nrows=100, ncols=100)
  w[] <- runif(ncell(w), min = 0, max = 100)
z <- raster(nrows=100, ncols=100)
  z[] <- runif(ncell(z), min = 0, max = 100)

My.fun <- function(x,y,z,w){
ifelse(x > y && x > w && x > z,
1,
ifelse(y > x && y > w && y > z,
2,
ifelse(w > x && w > y && w > z,
3,
ifelse(z > x && z > y && z > w,
4,NA),NA),NA),NA)
}

res <- overlay(x, y, z, w, fun = Vectorize(My.fun))
xz,
3.
如果是(z>x&&z>y&&z>w,
4,不适用),不适用),不适用)
}

res这里有几个问题。首先,您的
ifelse
语句有太多的参数(前3个语句末尾有一个不必要的
NA
。其次,您需要使用例如
x[]
而不是
x
来比较每个光栅中的数据。第三,您应该有一个
&
而不是
&

这样,您的函数就已经矢量化了:

My.fun y[]&x[]>w[]&x[]>z[],1,
如果是(y[]>x[]&y[]>w[]&y[]>z[],2,
如果是(w[]>x[]&w[]>y[]&w[]>z[],3,
ifelse(z[]>x[]&z[]>y[]&z[]>w[],4,NA)))
}
因此,您可以:

My.fun(x,y,w,z)
#>    [1] 3 1 3 2 3 4 2 3 1 3 4 4 3 4 1 2 2 1 2 3 4 3 4 4 1 1 3 2 3 1 2 4 2 4 2 4
#>   [37] 1 4 4 2 2 4 1 2 3 2 2 3 1 1 1 2 3 4 4 4 3 4 4 2 3 2 2 4 2 2 3 1 1 4 1 4
#>   [73] 2 2 2 3 3 2 1 4 1 3 3 4 4 4 4 1 4 3 2 1 4 1 1 2 3 1 3 3 1 3 4 4 1 1 4 3
#>  [109] 4 3 1 4 4 1 2 3 1 3 2 4 1 4 1 2 2 3 2 3 2 1 3 1 3 2 2 3 3 1 3 1 3 3 3 2
#>  [145] 1 4 4 3 2 1 3 1 3 1 1 1 4 2 3 1 1 4 2 3 1 3 2 2 2 2 3 1 1 3 4 1 2 1 1 2
#>  [181] 2 3 4 1 4 1 3 3 1 4 3 2 3 1 1 2 4 3 1 3 2 1 4 2 4 3 2 1 1 1 1 1 1 1 2 3
#>  [217] 4 4 2 1 2 2 1 3 2 3 3 3 4 3 2 1 2 2 4 2 4 4 2 2 4 3 4 3 1 2 4 4 4 3 2 2
#>  [253] 2 4 3 4 4 3 1 3 2 4 3 2 2 2 4 3 3 4 4 3 3 4 3 4 3 1 1 1 1 3 2 3 3 3 1 2
#>  [289] 1 4 4 4 3 4 3 4 3 2 2 3 1 3 1 1 1 1 3 2 4 4 4 1 1 3 4 4 4 3 4 1 2 1 1 4
#>  [325] 4 4 2 4 2 3 4 4 2 3 1 1 1 4 3 2 3 4 4 1 3 3 4 1 3 1 2 4 1 1 2 1 2 4 2 4
#>  [361] 3 3 2 2 1 1 4 2 1 3 4 4 3 1 2 2 3 4 2 4 2 3 1 4 3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3 2 3 2 2 3 2 4 1 1 4 4 2
#>  [865] 3 2 2 3 3 1 2 3 3 1 2 4 4 1 3 2 3 2 4 4 2 1 2 1 3 4 1 2 1 1 3 3 1 1 1 3
#>  [901] 3 4 2 1 1 4 4 4 2 1 3 3 3 1 4 3 1 2 3 1 1 1 3 2 1 4 1 1 4 1 4 1 1 3 1 2
#>  [937] 2 2 2 3 2 2 2 2 3 2 4 3 2 2 2 3 2 4 2 4 1 1 4 2 2 4 4 1 4 4 4 4 2 4 2 4
#>  [973] 4 1 4 3 1 2 3 1 3 2 3 3 2 1 3 1 2 1 2 3 1 3 4 3 3 2 3 2
#>[获取选项(“最大打印”)--省略9000个条目]

wxyz维度:10010010000(nrow、ncol、ncell)
#>分辨率:3.6,1.8(x,y)
#>范围:-180,180,-90,90(xmin,xmax,ymin,ymax)
#>crs:+proj=longlat+基准面=WGS84
#>资料来源:内存
#>名称:图层
#>值:1,4(最小值,最大值)

工作起来很有魅力。非常感谢你抽出时间,伙计。
wxyz <- raster(nrows=100, ncols=100)
wxyz[] <- My.fun(x,y,z,w)
wxyz
#> class      : RasterLayer 
#> dimensions : 100, 100, 10000  (nrow, ncol, ncell)
#> resolution : 3.6, 1.8  (x, y)
#> extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
#> crs        : +proj=longlat +datum=WGS84 
#> source     : memory
#> names      : layer 
#> values     : 1, 4  (min, max)