Skip to content

Commit cd0d3f3

Browse files
authored
descriptives: keep integer counts free of decimal places (#452)
Frequency counts were typed as 'number' when weights support was added, so even unweighted or integer-weighted counts displayed decimal places. Default the column to integer and switch to number only when the counts are actually non-integer. The case values are unavailable during init, so the type is determined in the run phase.
1 parent 4ba879d commit cd0d3f3

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

R/descriptives.b.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ descriptivesClass <- R6::R6Class(
627627

628628
for (var in tableVars)
629629
table$addColumn(name=var, title=var, type="text", combineBelow=TRUE)
630-
table$addColumn(name='counts', title=.('Counts'), type='number')
630+
table$addColumn(name='counts', title=.('Counts'), type='integer')
631631
table$addColumn(name='pc', title=.('% of Total'), type='number', format='pc')
632632
table$addColumn(name='cumpc', title=.('Cumulative %'), type='number', format='pc')
633633

@@ -997,6 +997,14 @@ descriptivesClass <- R6::R6Class(
997997
table <- tables$get(var)
998998
freq <- freqs[[var]]
999999

1000+
# the case values aren't available during init, so set the
1001+
# counts type here; only show decimals for non-integer counts
1002+
# (i.e. when weighted by non-integer weights)
1003+
if ( ! all(freq == round(freq), na.rm=TRUE)) {
1004+
countsColumn <- table$columns[['counts']]
1005+
countsColumn$.__enclos_env__$private$.type <- 'number'
1006+
}
1007+
10001008
tableVars <- c(var, splitBy)
10011009
allLevels <- lapply(jmvcore::select(self$data, tableVars), levels)
10021010
grid <- rev(expand.grid(rev(allLevels)))

tests/testthat/testdescriptives.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,19 @@ params <- list(
168168
list(
169169
weights = NULL,
170170
expected_counts = c(1, 1, 1, 1, 1),
171+
expected_type = "integer",
171172
info = "No weights"
172173
),
173174
list(
174175
weights = c(1, 2, 3, 4, 5),
175176
expected_counts = c(1, 2, 3, 4, 5),
177+
expected_type = "integer",
176178
info = "Integer weights"
177179
),
178180
list(
179181
weights = c(0.5, 1, 1.5, 2, 2.5),
180182
expected_counts = c(0.5, 1, 1.5, 2, 2.5),
183+
expected_type = "number",
181184
info = "Non-integer weights"
182185
)
183186
)
@@ -194,6 +197,10 @@ testthat::test_that("Weighted grouped frequency table is displayed correctly", {
194197
# THEN the counts in the frequency table are correct
195198
r <- desc$frequencies[[1]]$asDF
196199
testthat::expect_equal(r$counts, param$expected_counts, info = param$info)
200+
201+
# AND the counts are only displayed as decimals for non-integer weights
202+
countsType <- desc$frequencies[[1]]$getColumn("counts")$type
203+
testthat::expect_equal(countsType, param$expected_type, info = param$info)
197204
}
198205
})
199206

0 commit comments

Comments
 (0)