ZonalStat: Landscape Zonal Statistics

Description Usage Arguments Details Value Author(s) Examples

View source: R/ZonalStat.R

Description

ZonalStat calculates the statistics of data for specified zones of two matrices of data. The matrix can be a raster of class 'asc' (adehabitat package), 'RasterLayer' (raster package) or 'SpatialGridDataFrame' (sp package).

Usage

1
ZonalStat(mat, zones, FUN = "all")

Arguments

mat

a matrix of data to be summarized; The matrix can be a raster of class 'asc' (adehabitat package), 'RasterLayer' (raster package) or 'SpatialGridDataFrame' (sp package)

zones

a matrix of data with individual patches identified as with ConnCompLabel; The matrix must be of the same size & extent as mat

FUN

a single or vector of functions to be applied to each 'zone'; the default of 'all' will calculate min, 1st quarter, median, 3rd quarter, max, mean, standard deviation and n

Details

The code summarizes the data for defined zones. Nearly any function can be used for summarizing the data.

The FUN defined with 'all' as one of or the only function will append the functions of min, 1st quarter, median, 3rd quarter, max, mean, standard deviation and n to what is being calculated.

Value

a data.frame listing

zone

the unique ID for each zone.

functions...

a column for each of the functions identified

The data.frame will have an atribute defining the number of NA values that were excluded from the analysis.

Author(s)

Jeremy VanDerWal jjvanderwal@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#define a simple binary matrix
tmat = { matrix(c( 0,0,0,1,0,0,1,1,0,1,
                   0,0,1,0,1,0,0,0,0,0,
                   0,1,NA,1,0,1,0,0,0,1,
                   1,0,1,1,1,0,1,0,0,1,
                   0,1,0,1,0,1,0,0,0,1,
                   0,0,1,0,1,0,0,1,1,0,
                   1,0,0,1,0,0,1,0,0,1,
                   0,1,0,0,0,1,0,0,0,1,
                   0,0,1,1,1,0,0,0,0,1,
                   1,1,1,0,0,0,0,0,0,1),nr=10,byrow=TRUE) }

#do the connected component labelling
ccl.mat = ConnCompLabel(tmat)
ccl.mat #this is the zone matrix to be used

#create a random data matrix
data.mat = matrix(runif(100),nr=10,nc=10)
data.mat

#calculate the zonal statistics
zs.data = ZonalStat(data.mat,ccl.mat,FUN='all')
zs.data

#just calculate the sum
zs.data = ZonalStat(data.mat,ccl.mat,FUN='sum')
zs.data

#calculate sum & n & 'all' and show when a function is not defined
zs.data = ZonalStat(data.mat,ccl.mat,
    FUN=c('sum','length','not.a.function','all'))
zs.data
attr(zs.data,'excluded NAs') #show how many NAs were omitted from analysis

Example output

      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    0    0    0    1    0    0    2    2    0     3
 [2,]    0    0    1    0    1    0    0    0    0     0
 [3,]    0    1   NA    1    0    1    0    0    0     4
 [4,]    1    0    1    1    1    0    1    0    0     4
 [5,]    0    1    0    1    0    1    0    0    0     4
 [6,]    0    0    1    0    1    0    0    4    4     0
 [7,]    4    0    0    1    0    0    4    0    0     4
 [8,]    0    4    0    0    0    4    0    0    0     4
 [9,]    0    0    4    4    4    0    0    0    0     4
[10,]    4    4    4    0    0    0    0    0    0     4
            [,1]       [,2]      [,3]       [,4]      [,5]      [,6]       [,7]
 [1,] 0.76884800 0.83745332 0.1107789 0.76709430 0.7957211 0.4225583 0.32935889
 [2,] 0.58949326 0.85854725 0.1699911 0.10242354 0.8823623 0.2412267 0.43117917
 [3,] 0.12833774 0.84485780 0.1610602 0.80019489 0.1305523 0.5303676 0.11975163
 [4,] 0.83724574 0.12285171 0.4132693 0.96984468 0.5684178 0.4993081 0.25143925
 [5,] 0.82490473 0.75433304 0.6757207 0.64470354 0.3940144 0.3323959 0.19114855
 [6,] 0.63575689 0.11475749 0.8449961 0.24673027 0.1529660 0.4758089 0.10665030
 [7,] 0.40000207 0.06891454 0.2107623 0.08436056 0.7335747 0.6970967 0.06890609
 [8,] 0.78087339 0.82159443 0.3964697 0.92621867 0.9606366 0.9037777 0.77190531
 [9,] 0.72060185 0.41340809 0.1745448 0.71044331 0.6548495 0.3329684 0.13633552
[10,] 0.05212401 0.38070708 0.5329336 0.19787847 0.2012442 0.8181048 0.08376742
           [,8]      [,9]     [,10]
 [1,] 0.4488922 0.2188878 0.6961583
 [2,] 0.1548232 0.4080822 0.1614560
 [3,] 0.1568900 0.8454759 0.2878494
 [4,] 0.5067704 0.5195468 0.6990285
 [5,] 0.7596500 0.6457504 0.3521032
 [6,] 0.2233587 0.4679174 0.5800442
 [7,] 0.9425528 0.7683952 0.7444630
 [8,] 0.6266110 0.2963142 0.2799855
 [9,] 0.9650625 0.1712156 0.3968606
[10,] 0.4223220 0.8463830 0.2318233
  zones       max    qtr.75    median    qtr.25        min      mean         sd
1     0 0.9650625 0.7618363 0.4268687 0.1861653 0.06891454 0.4711920 0.28993373
2     1 0.9698447 0.8372457 0.6447035 0.3323959 0.08436056 0.5793435 0.29195371
3     2 0.4488922 0.4190089 0.3891256 0.3592422 0.32935889 0.3891256 0.08452284
4     3 0.6961583 0.6961583 0.6961583 0.6961583 0.69615825 0.6961583         NA
5     4 0.9037777 0.6769390 0.3968606 0.2559044 0.05212401 0.4412248 0.25402730
  length
1     60
2     17
3      2
4      1
5     19
  zones        sum
1     0 28.2715172
2     1  9.8488398
3     2  0.7782511
4     3  0.6961583
5     4  8.3832721
Warning message:
In ZonalStat(data.mat, ccl.mat, FUN = c("sum", "length", "not.a.function",  :
  not.a.function  is not a defined function!
  zones        sum length       max    qtr.75    median    qtr.25        min
1     0 28.2715172     60 0.9650625 0.7618363 0.4268687 0.1861653 0.06891454
2     1  9.8488398     17 0.9698447 0.8372457 0.6447035 0.3323959 0.08436056
3     2  0.7782511      2 0.4488922 0.4190089 0.3891256 0.3592422 0.32935889
4     3  0.6961583      1 0.6961583 0.6961583 0.6961583 0.6961583 0.69615825
5     4  8.3832721     19 0.9037777 0.6769390 0.3968606 0.2559044 0.05212401
       mean         sd
1 0.4711920 0.28993373
2 0.5793435 0.29195371
3 0.3891256 0.08452284
4 0.6961583         NA
5 0.4412248 0.25402730
[1] 1

SDMTools documentation built on Jan. 11, 2020, 9:23 a.m.

Related to ZonalStat in SDMTools...