From 736879bffb026d4c3489e604bbd4eabde2d19e5f Mon Sep 17 00:00:00 2001 From: valybionda Date: Fri, 26 Mar 2021 16:26:32 +0000 Subject: [PATCH] update argument names --- R/bc_compaction.R | 34 +++++++++++++++--------------- R/bc_decomp.R | 49 +++++++++++++++++++++++++++----------------- man/bc_compaction.Rd | 11 +++++++++- man/bc_decomp.Rd | 17 ++++++++++----- 4 files changed, 70 insertions(+), 41 deletions(-) diff --git a/R/bc_compaction.R b/R/bc_compaction.R index 64c9e62..1bae4ea 100644 --- a/R/bc_compaction.R +++ b/R/bc_compaction.R @@ -1,3 +1,5 @@ +#' @title bc_compaction +#' #' Calculates Percentage of core compression for cores #' #' Accepts a data.frame with core properties and returns a modified version @@ -9,25 +11,25 @@ #' @param external_distance name of the column with distance between sampler top and sediment surface #' @return the initial data.frame with the addition of Percentage of core compression -bc_compaction <- function( - data, - sampler_length, - internal_distance, - external_distance -){ - if(!is.data.frame(data)){ +bc_compaction <- + function(data, + sampler_length, + internal_distance, + external_distance) { + + # Stop if data is not a data.frame + if (!is.data.frame(data)) { stop("data is not a data.frame") } # Stop if any of the required variables are not numeric - if(!all(is.numeric(data[, sampler_length]), - is.numeric(data[, internal_distance]), - is.numeric(data[, external_distance]) - )){ - non_numeric <- !sapply( - X = list(data[, sampler_length], data[, internal_distance], data[, external_distance]), - FUN = is.numeric) + if (!all(is.numeric(data[, sampler_length]), + is.numeric(data[, internal_distance]), + is.numeric(data[, external_distance]))) { + non_numeric <- !sapply(X = list(data[, sampler_length], data[, internal_distance], data[, external_distance]), + FUN = is.numeric) - var_names <- c(sampler_length, internal_distance, external_distance) + var_names <- + c(sampler_length, internal_distance, external_distance) stop("The following variables are not numeric:\n", paste(var_names[which(non_numeric)], sep = "\n")) @@ -36,7 +38,7 @@ bc_compaction <- function( # estimate compaction correction factor compaction_correction_factor <- (data[, sampler_length] - data[, internal_distance]) / - (data[, sampler_length] - data[,external_distance]) + (data[, sampler_length] - data[, external_distance]) # compaction rate as percentage diff --git a/R/bc_decomp.R b/R/bc_decomp.R index 7eedc33..935bbb0 100644 --- a/R/bc_decomp.R +++ b/R/bc_decomp.R @@ -3,31 +3,42 @@ #' This function uses six arguments #' #' @param data dataframe with the following columns "ID" "cm" "weight" "LOI" "c_org". -#' @param tube_lenght The lenght in cm of the sampler. -#' @param core_in The lenght in cm of the part of the sampler left outside of the sediment (from the inside of the sampler). -#' @param core_out The lenght in cm of the part of the sampler left outside of the sediment (from the outside of the sampler). -#' @param diameter in cm of the sampler +#' @param sampler_lenght name of the column with the total length of the sampler tube +#' @param internal_distance The lenght in cm of the part of the sampler left outside of the sediment (from the inside of the sampler). +#' @param external_distance The lenght in cm of the part of the sampler left outside of the sediment (from the outside of the sampler). +#' @param sampler_diameter diameter in cm of the sampler #' @param method used to estimate the decompressed depth of each section, "linear" or "exp". Default is "linear". #' bc_decomp <- - function(data, tube_lenght, core_in, core_out, diameter, method = "linear") { - - if(!(method %in% c("linear", "exp"))) { - - return("Method must be either 'linear' or 'exp'") - } - - if(method == "linear") { + function(data, + sampler_lenght, + internal_distance, + external_distance, + sampler_diameter, + method = "linear") { + + # Stop if data is not a data.frame + if(!is.data.frame(data)){ + stop("data is not a data.frame") + } + + # Stop if method is not linear or exp + if (!(method %in% c("linear", "exp"))) { + return("Method must be either 'linear' or 'exp'") + } + + if (method == "linear") { + decomp <- data.frame(data$ID) - decomp$cm_obs <- data$cm + ((tube_lenght - core_out) - (tube_lenght - core_in)) + decomp$cm_obs <- data$cm + ((sampler_lenght - external_distance) - (sampler_lenght - internal_distance)) - corr_fact <- as.numeric((tube_lenght - core_in) / (tube_lenght - core_out)) + corr_fact <- as.numeric((sampler_lenght - internal_distance) / (sampler_lenght - external_distance)) decomp$cm_deco <- decomp$cm_obs * corr_fact decomp$sect_h <- c(dplyr::first(decomp$cm_deco), diff(decomp$cm_deco)) - decomp$volume <- (((pi * (diameter/2)^2) * decomp$sect_h)/2) #volume is divided by two as half section is used + decomp$volume <- (((pi * (sampler_diameter/2)^2) * decomp$sect_h)/2) #volume is divided by two as half section is used decomp$density <- data$weight/decomp$volume c <- as.numeric(stats::coef(stats::lm(c_org~LOI, data = data))[1]) @@ -41,18 +52,18 @@ bc_decomp <- } if(method == "exp") { - test <- data.frame(x = c(((tube_lenght - core_out) - (tube_lenght - core_in)), (tube_lenght - core_out)), - y = c(((tube_lenght - core_out) - (tube_lenght - core_in)), 0.1)) + test <- data.frame(x = c(((sampler_lenght - external_distance) - (sampler_lenght - internal_distance)), (sampler_lenght - external_distance)), + y = c(((sampler_lenght - external_distance) - (sampler_lenght - internal_distance)), 0.1)) a <- as.numeric(stats::coef(drc::drm(y~x, data=test, fct=aomisc::DRC.expoDecay()))[1]) b <- as.numeric(stats::coef(drc::drm(y~x, data=test, fct=aomisc::DRC.expoDecay()))[2]) decomp <- data.frame(data$ID) - decomp$cm_obs <- data$cm + ((tube_lenght - core_out) - (tube_lenght - core_in)) + decomp$cm_obs <- data$cm + ((sampler_lenght - external_distance) - (sampler_lenght - internal_distance)) decomp$cm_deco <- decomp$cm_obs - (a * exp(-b*decomp$cm_obs)) decomp$sect_h <- c(dplyr::first(decomp$cm_deco), diff(decomp$cm_deco)) - decomp$volume <- (((pi * (diameter/2)^2) * decomp$sect_h)/2) #volume is divided by two as half section is used + decomp$volume <- (((pi * (sampler_diameter/2)^2) * decomp$sect_h)/2) #volume is divided by two as half section is used decomp$density <- data$weight/decomp$volume c <- as.numeric(stats::coef(stats::lm(c_org~LOI, data = data))[1]) diff --git a/man/bc_compaction.Rd b/man/bc_compaction.Rd index a859deb..609c9fa 100644 --- a/man/bc_compaction.Rd +++ b/man/bc_compaction.Rd @@ -2,7 +2,12 @@ % Please edit documentation in R/bc_compaction.R \name{bc_compaction} \alias{bc_compaction} -\title{Calculates Percentage of core compression for cores} +\title{bc_compaction + +Calculates Percentage of core compression for cores + +Accepts a data.frame with core properties and returns a modified version +of it, with the addition of the estimated parameters} \usage{ bc_compaction(data, sampler_length, internal_distance, external_distance) } @@ -19,6 +24,10 @@ bc_compaction(data, sampler_length, internal_distance, external_distance) the initial data.frame with the addition of Percentage of core compression } \description{ +bc_compaction + +Calculates Percentage of core compression for cores + Accepts a data.frame with core properties and returns a modified version of it, with the addition of the estimated parameters } diff --git a/man/bc_decomp.Rd b/man/bc_decomp.Rd index 6a55c4a..d8be5d2 100644 --- a/man/bc_decomp.Rd +++ b/man/bc_decomp.Rd @@ -4,18 +4,25 @@ \alias{bc_decomp} \title{bc_decomp} \usage{ -bc_decomp(data, tube_lenght, core_in, core_out, diameter, method = "linear") +bc_decomp( + data, + sampler_lenght, + internal_distance, + external_distance, + sampler_diameter, + method = "linear" +) } \arguments{ \item{data}{dataframe with the following columns "ID" "cm" "weight" "LOI" "c_org".} -\item{tube_lenght}{The lenght in cm of the sampler.} +\item{sampler_lenght}{name of the column with the total length of the sampler tube} -\item{core_in}{The lenght in cm of the part of the sampler left outside of the sediment (from the inside of the sampler).} +\item{internal_distance}{The lenght in cm of the part of the sampler left outside of the sediment (from the inside of the sampler).} -\item{core_out}{The lenght in cm of the part of the sampler left outside of the sediment (from the outside of the sampler).} +\item{external_distance}{The lenght in cm of the part of the sampler left outside of the sediment (from the outside of the sampler).} -\item{diameter}{in cm of the sampler} +\item{sampler_diameter}{diameter in cm of the sampler} \item{method}{used to estimate the decompressed depth of each section, "linear" or "exp". Default is "linear".} }