Skip to content

Commit

Permalink
update argument names
Browse files Browse the repository at this point in the history
  • Loading branch information
costavale committed Mar 26, 2021
1 parent af29b08 commit 736879b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 41 deletions.
34 changes: 18 additions & 16 deletions R/bc_compaction.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"))
Expand All @@ -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
Expand Down
49 changes: 30 additions & 19 deletions R/bc_decomp.R
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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])
Expand Down
11 changes: 10 additions & 1 deletion man/bc_compaction.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 12 additions & 5 deletions man/bc_decomp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 736879b

Please sign in to comment.