Cross-tabulate
crosstab.Rd
Cross-tabulate the layers of a SpatRaster to create a contingency table.
Examples
r <- s <- rast(nc=5, nr=5)
set.seed(1)
values(r) <- runif(ncell(r)) * 2
values(s) <- runif(ncell(r)) * 3
x <- c(r, s)
crosstab(x)
#> lyr.1.1
#> lyr.1 0 1 2 3
#> 0 1 1 4 0
#> 1 2 5 5 0
#> 2 0 2 4 1
rs <- r/s
r[1:5] <- NA
s[20:25] <- NA
x <- c(r, s, rs)
crosstab(x, useNA=TRUE, long=TRUE)
#> lyr.1 lyr.1.1 lyr.1.2 n
#> 1 0 2 0 3
#> 2 0 NA 0 1
#> 3 0 NA 6 1
#> 4 1 0 4 1
#> 5 1 1 1 1
#> 6 1 1 2 1
#> 7 1 2 0 3
#> 8 1 2 1 1
#> 9 1 NA 0 1
#> 10 1 NA 1 1
#> 11 2 1 1 2
#> 12 2 2 1 2
#> 13 2 NA 1 2
#> 14 NA 0 19 1
#> 15 NA 1 0 2
#> 16 NA 1 1 1
#> 17 NA 3 1 1