Which returns a RasterLayer with TRUE or FALSE setting cells that are NA to FALSE (unless na.rm=FALSE). If the RasterLayer has numbers, all values that are 0 become FALSE and all other values become TRUE. The function can also return the cell numbers that are TRUE

# S4 method for RasterLayer
Which(x, cells=FALSE, na.rm=TRUE, ...)

Arguments

x

RasterLayer

cells

logical. If TRUE, cell numbers are returned, otherwise a RasterLayer is returned

na.rm

logical. If TRUE, NA values are treated as FALSE, otherwise they remain NA (only when cells=FALSE)

...

Additional arguments (none implemented)

See also

Value

RasterLayer

Examples

r <- raster(ncol=10, nrow=10)
set.seed(0)
values(r) <- runif(ncell(r))
r[r < 0.2 ] <- 0
r[r > 0.8] <- 1
r[r > 0 & r < 1 ] <- 0.5

Which(r, cells=TRUE)
#>  [1]   1   2   3   4   5   6   7   8   9  10  12  14  15  16  17  18  19  20  21
#> [20]  22  23  24  26  27  29  30  31  32  33  34  36  37  38  40  41  42  43  44
#> [39]  45  46  47  49  50  51  52  53  54  55  58  59  60  61  62  63  64  65  66
#> [58]  67  68  69  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86
#> [77]  87  88  90  92  94  95  96  97  98  99 100
Which(r > 0.5, cells=TRUE)
#>  [1]   1   5   7   8  19  22  30  36  42  53  62  71  73  77  78  81  95 100

s1 <- r > 0.5
s2 <- Which(r > 0.5)
s1[1:15]
#>  [1] 1 0 0 0 1 0 1 1 0 0 0 0 0 0 0
s2[1:15]
#>  [1] 1 0 0 0 1 0 1 1 0 0 0 0 0 0 0

# this expression
x1 <- Which(r, na.rm=FALSE)
# is the inverse of
x2 <- r==0