Skip to contents

A fast data.table-based alternative to raster::freq().

Usage

freqDT(x, ...)

# S4 method for RasterLayer
freqDT(x, digits = 0, value = NULL, useNA = c("ifany", "no", "always"), ...)

# S4 method for RasterStackBrick
freqDT(
  x,
  digits = 0,
  value = NULL,
  useNA = c("ifany", "no", "always"),
  merge = FALSE,
  ...
)

Arguments

x

A RasterLayer, RasterStack, or RasterBrick object field class.

...

Additional arguments as for raster::writeRaster(), on which this function relies.

digits

Integer for rounding the cell values. Argument is passed to round

value

A single numeric, logical, or NA value. If supplied, freqDT() will only count the number of cells with that value.

useNA

Character (one of "no", "ifany", or "always"). What to do with NA values? See table for details.

merge

Logical. If TRUE the list will be merged into a single data.table.

Author

Joshua O'Brien

Examples

r <- raster(nrow = 18, ncol = 36)
r[] <- runif(ncell(r))
r[1:5] <- NA
r <- r * r * r * 5
freqDT(r)
#>    ID freq
#> 1: NA    5
#> 2:  0  293
#> 3:  1  134
#> 4:  2   75
#> 5:  3   77
#> 6:  4   46
#> 7:  5   18

freqDT(r, value = 2)
#> [1] 75

s <- stack(r, r*2, r*3)
freqDT(s, merge = TRUE)
#>     ID layer.1 layer.2 layer.3
#>  1: NA       5       5       5
#>  2:  0     293     233     209
#>  3:  1     134     104      84
#>  4:  2      75      57      49
#>  5:  3      77      55      40
#>  6:  4      46      43      45
#>  7:  5      18      37      28
#>  8:  6      NA      36      29
#>  9:  7      NA      27      18
#> 10:  8      NA      21      32
#> 11:  9      NA      19      24
#> 12: 10      NA      11      21
#> 13: 11      NA      NA      15
#> 14: 12      NA      NA      15
#> 15: 13      NA      NA      16
#> 16: 14      NA      NA      14
#> 17: 15      NA      NA       4