Last data update: 2014.03.03

R: Landscape Zonal Statistics
ZonalStatR Documentation

Landscape Zonal Statistics

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

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

#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

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(SDMTools)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/SDMTools/ZonalStat.Rd_%03d_medium.png", width=480, height=480)
> ### Name: ZonalStat
> ### Title: Landscape Zonal Statistics
> ### Aliases: ZonalStat
> 
> ### ** Examples
> 
> #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
      [,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
> 
> #create a random data matrix
> data.mat = matrix(runif(100),nr=10,nc=10)
> data.mat
           [,1]       [,2]      [,3]      [,4]       [,5]      [,6]       [,7]
 [1,] 0.3826785 0.93482608 0.9074204 0.4910216 0.09654837 0.7017864 0.59341128
 [2,] 0.1036399 0.78147617 0.7831651 0.4957784 0.23268018 0.4673483 0.95461315
 [3,] 0.0592106 0.28942695 0.4397057 0.8915304 0.12683895 0.1579520 0.49341280
 [4,] 0.8166142 0.65700501 0.5761739 0.2805436 0.96888028 0.3899954 0.38113616
 [5,] 0.8688322 0.55877212 0.4107017 0.8979340 0.67299772 0.8020512 0.95480874
 [6,] 0.1477310 0.49142933 0.6814985 0.2317230 0.42067132 0.2575739 0.64228302
 [7,] 0.6873200 0.86514993 0.9469325 0.6366806 0.71383963 0.7735655 0.14990733
 [8,] 0.3509217 0.73828773 0.2952856 0.2718982 0.46570111 0.4446851 0.22850124
 [9,] 0.1535735 0.07920304 0.5604754 0.5733669 0.67075221 0.6232712 0.35890016
[10,] 0.5041305 0.66356086 0.4333229 0.1063331 0.69341953 0.2971841 0.01585561
           [,8]       [,9]     [,10]
 [1,] 0.9666862 0.05332707 0.3737084
 [2,] 0.4450834 0.53984093 0.8341625
 [3,] 0.2000877 0.49821417 0.5348406
 [4,] 0.8790093 0.46354686 0.6736775
 [5,] 0.3165657 0.79892067 0.4864012
 [6,] 0.3162662 0.60670478 0.6430678
 [7,] 0.3111576 0.32854828 0.2583772
 [8,] 0.9650723 0.66391491 0.5365642
 [9,] 0.8700556 0.49294583 0.2134934
[10,] 0.4819604 0.75394965 0.5391242
> 
> #calculate the zonal statistics
> zs.data = ZonalStat(data.mat,ccl.mat,FUN='all')
> zs.data
  zones       max    qtr.75    median    qtr.25        min      mean        sd
1     0 0.9650723 0.7238671 0.4866949 0.2894388 0.01585561 0.5001604 0.2819934
2     1 0.9688803 0.8020512 0.5761739 0.3811362 0.15795200 0.5803960 0.2568583
3     2 0.9666862 0.8733674 0.7800487 0.6867300 0.59341128 0.7800487 0.2639452
4     3 0.3737084 0.3737084 0.3737084 0.3737084 0.37370843 0.3737084        NA
5     4 0.7382877 0.6351328 0.5365642 0.4390040 0.14990733 0.5048031 0.1677481
  length
1     60
2     17
3      2
4      1
5     19
> 
> #just calculate the sum
> zs.data = ZonalStat(data.mat,ccl.mat,FUN='sum')
> zs.data
  zones        sum
1     0 30.0096255
2     1  9.8667321
3     2  1.5600974
4     3  0.3737084
5     4  9.5912583
> 
> #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'))
Warning message:
In ZonalStat(data.mat, ccl.mat, FUN = c("sum", "length", "not.a.function",  :
  not.a.function  is not a defined function!
> zs.data
  zones        sum length       max    qtr.75    median    qtr.25        min
1     0 30.0096255     60 0.9650723 0.7238671 0.4866949 0.2894388 0.01585561
2     1  9.8667321     17 0.9688803 0.8020512 0.5761739 0.3811362 0.15795200
3     2  1.5600974      2 0.9666862 0.8733674 0.7800487 0.6867300 0.59341128
4     3  0.3737084      1 0.3737084 0.3737084 0.3737084 0.3737084 0.37370843
5     4  9.5912583     19 0.7382877 0.6351328 0.5365642 0.4390040 0.14990733
       mean        sd
1 0.5001604 0.2819934
2 0.5803960 0.2568583
3 0.7800487 0.2639452
4 0.3737084        NA
5 0.5048031 0.1677481
> attr(zs.data,'excluded NAs') #show how many NAs were omitted from analysis
[1] 1
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>