Skip to content

Commit

Permalink
Fix issue with calculation of subtotals to allow for differences as w…
Browse files Browse the repository at this point in the history
…ell as sums
  • Loading branch information
domjarkey committed Sep 28, 2022
1 parent e5b0e41 commit 2cf96a1
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions R/forNowTransforms.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,34 @@ calcTabInsertions <- function(vec, elements, var_cats) {
# if element is a subtotal, sum the things it corresponds to which are
# found with arguments()
if (crunch::is.Subtotal(element)) {
# grab category combinations, and then sum those categories.
combos <- element$categories
which.cats <- names(var_cats)[crunch::ids(var_cats) %in% combos]
if (any(is.na(var_cats)[crunch::ids(var_cats) %in% combos])) {
return(NA) # nocov
# Check if subtotal is strictly a sum of variables or a sum/difference
if (is.null(element$negative) || length(element$negative) == 0) {
# grab category combinations, and then sum those categories.
combos <- element$categories
which.cats <- names(var_cats)[crunch::ids(var_cats) %in% combos]
if (any(is.na(var_cats)[crunch::ids(var_cats) %in% combos])) {
return(NA) # nocov
}
if (dim(vec)[2] == 1) {
return(sum(vec[which.cats, ]))
}
return(colSums(vec[which.cats, , drop = FALSE]))
} else {
# if element has a "negative" item, these need to be subtracted from
# the other categories
# grab category combinations, and then sum those categories.
combos <- element$categories
combos_negative <- as.integer(unlist(element$negative))
which.cats <- names(var_cats)[crunch::ids(var_cats) %in% combos]
which.cats_negative <- names(var_cats)[crunch::ids(var_cats) %in% combos_negative]
if (any(is.na(var_cats)[crunch::ids(var_cats) %in% c(combos, combos_negative)])) {
return(NA) # nocov
}
if (dim(vec)[2] == 1) {
return(sum(vec[which.cats, ]) - sum(vec[which.cats_negative, ]))
}
return(colSums(vec[which.cats, , drop = FALSE]) - colSums(vec[which.cats_negative, , drop = FALSE]))
}
if (dim(vec)[2] == 1) {
return(sum(vec[which.cats, ]))
}
return(colSums(vec[which.cats, , drop = FALSE]))
}
}))

Expand Down

0 comments on commit 2cf96a1

Please sign in to comment.