Skip to content

Commit

Permalink
fixed bug in percentage calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
zoyafuso-NOAA committed Oct 18, 2024
1 parent 0d0fc3c commit 271d98e
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions functions/calc_diff.R
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

0 comments on commit 271d98e

Please sign in to comment.