R/CalculateCarbonContent.R

#' @title Function for calculating biological mass/C content of soil cores.
#'
#' @description
#'
#' This takes a very specific format dataframe of ashed sample weights
#' and calculates biomass/C content of cores.
#'
#' @param data           Input dataframe. See 'details'.
#' @param core.length    Length of soil core, in centimeters.
#' @param core.diameter  Diameter of soil core, in centimeters.
#'
#' @return
#'
#' Returns a dataframe. carbon_weight is in grams, biodensity is in g per cm3 soil,
#' carbon_density is in g per cm3 soil, carbon fraction is unitless percent.
#'
#' @details
#'
#' \code{data} currently requires several columns, including:
#'
#' \code{dry_biomass} is a column of dry weights, by sample, in grams.
#'
#' \code{ash_weight} is a column of ash weights, by sample, in grams.
#'
#'
#' @export
#' @author Brandon McNellis
#' @examples
#' crappy_unit_test <- CalculateCarbonContent(data = data)
CalculateCarbonContent <- function(data, core.length = 14, core.diameter = 2.4) {
  # Input check section ####
  stopifnot(
    class(data) == "data.frame",
    class(core.length) == "numeric",
    class(core.diameter) == "numeric",
    "dry_biomass" %in% colnames(data),
    "ash_weight" %in% colnames(data),
    "sample" %in% colnames(data),
    "treatment" %in% colnames(data)
  )
  # Preliminary calculations ####
  # Calculate volume of soil corer
  core.volume <- pi * ((core.diameter / 2) ^ 2) * core.length
  # Calculate C attributes
  carbon_weight <- data[["dry_biomass"]] - data[["ash_weight"]]
  carbon_fraction <- carbon_weight / data[["dry_biomass"]]
  # Calculate bio/C density
  biodensity <- data[["dry_biomass"]] / core.volume # Returns g/cm3
  carbon_density <- carbon_weight / core.volume # returns gC/cm3
  # Update input dataframe
  data <- data.frame(data, carbon_weight, carbon_fraction, biodensity, carbon_density)
  return(data)
}
bmcnellis/SDEF.analysis documentation built on June 4, 2019, 10 a.m.