From 6fd049b11e32048071e2a3d1924b18eb4d57dd8f Mon Sep 17 00:00:00 2001 From: Miguel Alvarez Date: Fri, 28 Oct 2016 00:10:24 +0200 Subject: [PATCH] Function curve_area --- DESCRIPTION | 2 +- NAMESPACE | 1 + R/sloss.R | 6 +++--- man/curve_area.Rd | 47 +++++++++++++++++++++++++++++++++++++++++++++++ man/gap_fill.Rd | 2 ++ 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 man/curve_area.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 28040a2..5194ddb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: Lexiguel -Version: 0.0.0.9008 +Version: 0.0.0.9009 Date: 2016-10-27 Title: Miscellaneous Functions Authors@R: person("Miguel", "Alvarez", email="malvarez@uni-bonn.de", diff --git a/NAMESPACE b/NAMESPACE index a7b1e9c..5fb4367 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,7 @@ importFrom("stats", "terms") ## Export regular function names export( + curve_area, error_bars, gap_id, gap_fill, diff --git a/R/sloss.R b/R/sloss.R index d57a3bc..df65cf1 100644 --- a/R/sloss.R +++ b/R/sloss.R @@ -4,9 +4,9 @@ ################################################################################ # Internal function calculating area below the curve (trapezoids) -curve_area <- function(x, y) { - D1 <- c(0, diff(x)) - D2 <- c(0, diff(y)) +curve_area <- function(x, y, bottom=0) { + D1 <- c(bottom, diff(x)) + D2 <- c(bottom, diff(y)) Area <- sum(D1*y + D1*D2/2) return(Area) } diff --git a/man/curve_area.Rd b/man/curve_area.Rd new file mode 100644 index 0000000..9d20a73 --- /dev/null +++ b/man/curve_area.Rd @@ -0,0 +1,47 @@ +\name{curve_area} +\alias{curve_area} + +\title{Calculation of area below a curve.} +\description{ +This function calculates the area below a curve by the trapezoidal rule. +} +\usage{ +curve_area(x, y, bottom=0) +} +\arguments{ + \item{x}{A numerical vector with the ordinate values.} + \item{y}{A numerical vector with the abscissa values.} + \item{bottom}{A numerical value indicating the bottom for area calculation.} +} +\details{ +The argument \code{bottom} can be adjusted to the minimum observed values an +will be particularly useful when values of \code{y} are negative. + +Curves going bellow the \code{bottom} value will get a negative value of area +and therefore subtracted when \code{y} cuts the bottom value. + +This function was originally written as internal function for the calculations +done by \code{\link{sloss}}. +} +\value{ +A numeric value. +} +\author{ +Miguel Alvarez (\email{malvarez@uni-bonn.de}). +} +\examples{ +## Load gaps from the Robinson Crusoe Island +library(Lexiguel) +data(rc_gaps) +data(rc_gaps.env) + +## Calculation of curves +rc_curves <- sloss(rc_gaps, rc_gaps.env, area) + +## Area calculated by function +rc_curves$Index + +## Cross-check +with(rc_curves$SL, curve_area(area, species))/with(rc_curves$LS, + curve_area(area, species)) +} diff --git a/man/gap_fill.Rd b/man/gap_fill.Rd index 70018f5..792fbc6 100644 --- a/man/gap_fill.Rd +++ b/man/gap_fill.Rd @@ -35,6 +35,8 @@ is the vector \code{x} with NAs replaced by custom values. Miguel Alvarez (\email{malvarez@uni-bonn.de}). } \examples{ +library(Lexiguel) + ## A vector with two gaps A <- c(1:5, NA, NA, 7:10, NA, NA, NA, 16:20) A