Learn R Programming

GeNetIt (version 0.1-6)

graph.statistics: Statistics for edges (lines)

Description

Extracts raster values for each edge and calculates specified statistics

Usage

graph.statistics(x, r, stats = c("min", "mean", "max"), buffer = NULL)

Value

data.frame object of statistics

Arguments

x

sp SpatialLinesDataFrame or sf LINE object

r

A terra SpatRast or raster rasterLayer, rasterStack, rasterBrick object

stats

Statistics to calculate. If vectorized, can pass a custom statistic function.

buffer

Buffer distance, radius in projection units. For statistics based on edge buffer distance

Author

Jeffrey S. Evans <jeffrey_evans@tnc.org> and Melanie A. Murphy <melanie.murphy@uwyo.edu>

Examples

Run this code
# \donttest{
 library(sf)
 library(terra)  

 data(ralu.site)	
 xvars <- rast(system.file("extdata/covariates.tif", package="GeNetIt"))
 
  ( dist.graph <- knn.graph(ralu.site, row.names = ralu.site$SiteName, 
                            max.dist = 1500) )
 
 skew <- function(x, na.rm = TRUE) {  
           if (na.rm) x <- x[!is.na(x)]
           sum( (x - mean(x)) ^ 3) / ( length(x) * sd(x) ^ 3 )  
 		}

# Moments on continuous raster data
 system.time( {		
  stats <- graph.statistics(dist.graph, r = xvars[[-6]],  
              stats = c("min", "median", "max", "var", "skew")) 
 } ) 

# Proportional function on nominal raster data		
p <- function(x) { length(x[x < 52]) / length(x) }	
	
  system.time( {		
   nstats <- graph.statistics(dist.graph, r = xvars[[6]],
               stats = "p") 
  } ) 	

# Based on 500m buffer distance around line(s)
 system.time( {		
  stats <- graph.statistics(dist.graph, r = xvars[[-6]],  
              stats = c("min", "median", "max", "var", "skew"),
			      buffer = 500) 
 } )

# }

Run the code above in your browser using DataLab