Skip to content

Commit

Permalink
Merge pull request #58 from zoyafuso-NOAA/main
Browse files Browse the repository at this point in the history
Quick fixes after 10/17/24 run
  • Loading branch information
zoyafuso-NOAA authored Oct 18, 2024
2 parents 90f25c2 + 16ebbb5 commit 5c2c3fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
7 changes: 4 additions & 3 deletions code/utility_scripts/taxonomic_groupings.R
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,12 @@ test_check <- merge(x = updated_gp_taxon_groups,
## Reorder columns
test_check <-
test_check[, c("SPECIES_CODE",
as.vector(sapply(X = names(current_gp_taxon_groups)[-2],
as.vector(sapply(X = names(current_gp_taxon_groups)[-1],
FUN = function(x)
paste0(x, c("_UPDATE", "_CURRENT")))))]

## Check for differences in each non-key field
for (icol in names(current_gp_taxon_groups)[-2]) {
for (icol in names(current_gp_taxon_groups)[-1]) {
test_check[, paste0(icol, "_DIFF")] <-
test_check[, paste0(icol, "_UPDATE")] !=
test_check[, paste0(icol, "_CURRENT")]
Expand All @@ -294,7 +295,7 @@ for (icol in names(current_gp_taxon_groups)[-2]) {
## Reorder columns
test_check <-
test_check[, c("SPECIES_CODE",
as.vector(sapply(X = names(current_gp_taxon_groups)[-2],
as.vector(sapply(X = names(current_gp_taxon_groups)[-1],
FUN = function(x)
paste0(x, c("_UPDATE",
"_CURRENT",
Expand Down
2 changes: 1 addition & 1 deletion functions/calc_diff.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ calc_diff <- function(v1, v2, percent = T) {
"DIFF" = v2 - v1)
data.table::setnames(x = df, c("v1", "v2", "DIFF"))

df[, v2 := ifelse(test = v2 == 0, 1, v2)]
df[, v1 := ifelse(test = v1 == 0, 1, v1)]
df[, PERC_DIFF := 100 * DIFF / v1]

return(df[, ifelse(percent == T, "PERC_DIFF", "DIFF"), with = F])
Expand Down
47 changes: 35 additions & 12 deletions functions/compare_tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,22 @@ compare_tables <- function(x = NULL,

for (icol in 1:nrow(x = cols_to_check)) {
x[, paste0(cols_to_check$colname[icol], "_DIFF") :=
round(x = calc_diff(v1 = x[, paste0(cols_to_check$colname[icol],
base_table_suffix), with = F],
v2 = x[, paste0(cols_to_check$colname[icol],
update_table_suffix), with = F],
percent = cols_to_check$percent[icol]) ,
digits = cols_to_check$decplaces[icol])]
round(x = calc_diff(v1 = x[, paste0(cols_to_check$colname[icol],
base_table_suffix), with = F],
v2 = x[, paste0(cols_to_check$colname[icol],
update_table_suffix), with = F],
percent = cols_to_check$percent[icol]) ,
digits = cols_to_check$decplaces[icol])]
}

new_records_stmt <-
paste0("x[",
paste0(sapply(X = cols_to_check$colname,
FUN = function(x)
paste0("(is.na(x = ", x, base_table_suffix,
") & !is.na(x = ", x, update_table_suffix,
"))")),
collapse = "|"),
collapse = "&"),
"]")
new_records <- eval(parse(text = new_records_stmt))
new_records <- new_records[, c(key_columns, col_order), with = F]
Expand All @@ -94,16 +94,39 @@ compare_tables <- function(x = NULL,
removed_records <- eval(parse(text = removed_records_stmt))
removed_records <- removed_records[, c(key_columns, col_order), with = F]

modified_records_stmt <-
## modified records consist of two types:
## 1) non-zero differences
## 2) instances where
modified_records_stmt1 <-
paste0("x[",
paste0(sapply(X = cols_to_check$colname,
FUN = function(x)
paste0(x, "_DIFF != 0")),
collapse = " | "),
"]")
modified_records <- eval(parse(text = modified_records_stmt))
modified_records <- modified_records[, c(key_columns, col_order), with = F]

modified_records1 <- eval(parse(text = modified_records_stmt1))

modified_records_stmt2 <-
paste0("x[",
paste0(sapply(X = cols_to_check$colname,
FUN = function(x)
paste0("(is.na(x = ", x, base_table_suffix,
") & !is.na(x = ", x, update_table_suffix,
"))")),
collapse = "|"),
"]")
modified_records2 <- eval(parse(text = modified_records_stmt2))
modified_records2 <- modified_records2[
apply(X = modified_records2[,
paste0(cols_to_check$colname, "_DIFF"),
with = F],
MARGIN = 1,
FUN = function(x) !all(is.na(x)))]


modified_records <- rbind(modified_records1, modified_records2)
modified_records <- modified_records[, c(key_columns, col_order), with = F]

return(do.call(what = list,
args = list(new_records = new_records,
removed_records = removed_records,
Expand Down

0 comments on commit 5c2c3fd

Please sign in to comment.