Skip to content

Commit 0866452

Browse files
committed
Fix problem with aggregation functions
1 parent 4f9dc61 commit 0866452

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: er.helpers
22
Type: Package
33
Title: Helper functions commonly used in Environmental Reporting at Stats NZ
4-
Version: 1.1.1
4+
Version: 1.1.2
55
Authors@R: c(
66
person("Fernando", "Cagua", email = "fernando.cagua@stats.govt.nz", role = c("aut", "cre")),
77
person("David", "Hodge", email = "david.hodge@stats.govt.nz", role = c("aut")),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## er.helpers 1.1.1
2+
3+
* Bug fix: aggregation functions now return NA as expected when all values are missing. Even if it passes other requirements.
4+
15
## er.helpers 1.1.0
26

37
* New feature: Added all palettes from simplevis, plus a signed square root trans function scales.

R/aggregation.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ aggregate_with_criteria <- function(x, max_missing = 0, max_consecutive = 0, fun
2828
n_missing <- sum(na_values)
2929
prop_missing <- n_missing / length(x)
3030

31+
# Don't need to calculate anything else if everything is missing
32+
if (prop_missing == 1)
33+
return(NA)
34+
3135
# If max missing is a proportion
3236
if (max_missing <= 1 & max_missing > 0) {
3337
if (prop_missing > max_missing)

tests/testthat/test-aggregation.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
test_that("aggregation functions work", {
2+
3+
# Test that functions return NA when everything is NA, even if the number is
4+
# below requirements
5+
expect_equal(mean_with_criteria(rep(NA), 10, 4), NA)
6+
expect_equal(min_with_criteria(rep(NA), 10, 4), NA)
7+
expect_equal(max_with_criteria(rep(NA), 10, 4), NA)
8+
})

0 commit comments

Comments
 (0)