From 271d98ed241630ca9492d3fa116667ea04da53e0 Mon Sep 17 00:00:00 2001 From: zoyafuso-NOAA <60302777+zoyafuso-NOAA@users.noreply.github.com> Date: Thu, 17 Oct 2024 22:06:22 -0700 Subject: [PATCH] fixed bug in percentage calculation --- functions/calc_diff.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/calc_diff.R b/functions/calc_diff.R index c5a0e485..67c4b942 100644 --- a/functions/calc_diff.R +++ b/functions/calc_diff.R @@ -11,11 +11,11 @@ calc_diff <- function(v1, v2, percent = T) { df <- data.table::data.table("v1" = v1, "v2" = v2, - "DIFF" = v1 - v2) + "DIFF" = v2 - v1) data.table::setnames(x = df, c("v1", "v2", "DIFF")) df[, v2 := ifelse(test = v2 == 0, 1, v2)] - df[, PERC_DIFF := DIFF / v2] + df[, PERC_DIFF := 100 * DIFF / v1] return(df[, ifelse(percent == T, "PERC_DIFF", "DIFF"), with = F]) }