Learn R Programming

spatialEco (version 2.0-2)

mean_angle: Mean Angle

Description

Calculates the mean angle of a vector

Usage

mean_angle(a, angle = c("degree", "radians"))

Value

A vector of mean angle

Arguments

a

vector of angle values

angle

("degree", "radians") to define angle in degrees or radians

Author

Jeffrey S. Evans <jeffrey_evans@tnc.org>

Details

The arithmetic mean is not correct for calculating the central tendency of angles. This function is intended to return the mean angle for slope or aspect, which could be used in a focal or zonal function.

Examples

Run this code
library(terra)
mean_angle(c(180, 10))
  mean(c(180, 10))
mean_angle(c(90, 180, 70, 60))
  mean(c(90, 180, 70, 60))
mean_angle(c(90, 180, 270, 360))
  mean(c(90, 180, 270, 360))

elev <- rast(system.file("extdata/elev.tif", package="spatialEco"))
asp <- terrain(elev, v="aspect")
s <- buffer(spatSample(asp, 20, as.points=TRUE, 
            na.rm=TRUE, values=FALSE), 5000)

plot(asp)
  plot(s, add=TRUE)

d <- extract(asp, s)
cat("Mean angles of aspect", "\n")
  tapply(d[,2], d[,1], mean_angle) 
cat("arithmetic means of aspect", "\n")
  tapply(d[,2], d[,1], mean, na.rm=TRUE) 

Run the code above in your browser using DataLab