简体   繁体   English

如何在R中编写switch语句?

[英]How can I write a switch statement in R?

I am not able to write a switch statement correctly. 我无法正确编写switch语句。 And the examples available are not helpful. 并且可用的示例没有帮助。 Basically, the statement should check what the value in a cell is (Choices: 1,2,4,8,16,32,64,128) and based on that increase/decrease the row and column number. 基本上,该语句应检查单元格中的值是什么(选择:1、2、4、8、16、32、64、128),并基于该增加/减少行和列号。 Some switch statements have 2 calculations. 某些switch语句具有2个计算。

library(raster)
fdr<-raster("fdr.tif")
row1<-50
col1<-50
cell1<-fdr[row1,col1] #The point of origin
switch (cell1,
        4={row2 = row1 + 1
        },
        2={row2 = row1 + 1 & col2 = col1 + 1
        },
        1={col2 = col1 + 1
        },
        128={row2 = row1 - 1 & col2 = col1 + 1
        },
        64={row2 = row1 - 1
        },
        32={row2 = row1 - 1 & col2 = col1 - 1
        },
        16={col2 = col1 - 1
        },
        8={row2 = row1 + 1 & col2 = col1 - 1
        }
)

I am open to techniques other than a switch statement as well. 除了switch语句外,我还接受其他技术。 Or any idea to make it faster. 或任何使其更快的想法。 Eventually I'll wrap this into a function and do it for every cell (pixel). 最终,我将其包装到一个函数中,并为每个单元(像素)执行该操作。

Data: 数据:

  1. FDR raster (small file): https://www.dropbox.com/s/7o3y8w01y6zqqwm/fdr.tif?dl=0 FDR栅格(小文件): https : //www.dropbox.com/s/7o3y8w01y6zqqwm/fdr.tif?dl=0

  2. The concept is water flow direction: http://courses.washington.edu/gis250/lessons/hydrology/index.html#coded 这个概念是水流的方向: http//courses.washington.edu/gis250/lessons/hydrology/index.html#coded

This is how you write your switch and get it to return a vector of row, col: 这是编写switch并使其返回行向量col的方式:

row1 <- 50
col1 <- 50
cell1 <- 16

rowcol <- switch(as.character(cell1),
                 '4' = c(row1 + 1, col1),
                 '2'   = c(row1 + 1, col1 + 1),
                 '1'   = c(row1, col1 + 1),
                 '128' = c(row1 - 1, col1 + 1),
                 '64'  = c(row1 - 1, col1),
                 '32'  = c(row1 - 1, col1 - 1),
                 '16'  = c(row1, col1 - 1),
                 '8'   = c(row1 + 1, col1 - 1))

rowcol

> rowcol
[1] 50 49

You don't want to be doing a switch for each element of a large raster repeatedly; 您不想重复对大型栅格的每个元素进行switch it will be glacial. 它将是冰河。 Here is a vectorised operation that starts from any row/col and does a single update/move 这是一个矢量化的操作,它从任何行/列开始,并执行一次更新/移动

## generate some dummy raster data
set.seed(1)
m <- matrix(sample(2^(0:7), 9, replace = TRUE), ncol = 3)

## collect row and column indices for raster
dd <- cbind(r = as.vector(row(m)), c = as.vector(col(m)))

## look-up matrix for row and col shifts
lu <- matrix(c( 0,  1,
                1,  1,
                1,  0,
                1, -1,
                0, -1,
               -1, -1,
               -1,  0,
               -1,  1), ncol = 2, byrow = TRUE)
## set rownames with powers of 2 to allow indexing using `m`
rownames(lu) <- 2^(0:7)

## need `m` as a vector
mc <- as.character(as.vector(m))

## which cell to move to next given values of `m`
move <- dd + lu[mc, ]
move

which gives 这使

> move
      r c
 [1,] 2 1
 [2,] 3 1
 [3,] 3 0
 [4,] 0 3
 [5,] 3 3
 [6,] 2 3
 [7,] 0 4
 [8,] 1 2
 [9,] 2 2

Which is correct given the input data. 给定输入数据,这是正确的。 Now you have the problem of making the next move and keeping track of things. 现在,您面临着下一步行动并保持跟踪的问题。 In this example, the move would shift you to cells outside the raster for 3 of the 9 cells (the ones with a 0 in r or c and hence if you indexed back into m to get the power of two in the cell moved too , you only get 6 values returned 在此示例中,移动会将您转移到9个像元中有3个的栅格外部像元( rc为0的像元,因此,如果您索引回m以获取移动像元中2的幂,您只会得到6个值

> m[move]
[1]   4  16  32  32 128   2

Hence you'd need to keep track of this as you walk, but this should get you started. 因此,您需要在行走时保持对此的跟踪,但这应该可以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM