Skip to content

Commit

Permalink
Merge pull request #164 from OHDSI/fixNanMinCellValue
Browse files Browse the repository at this point in the history
Censoring all values to NA where specified the minCellValue is NaN
  • Loading branch information
schuemie authored Apr 17, 2024
2 parents 8ba141d + 1163132 commit bd904fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
5 changes: 4 additions & 1 deletion R/Export.R
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ enforceMinCellValue <- function(data, fieldName, minValues, silent = FALSE) {
" because value below minimum"
)
}
if (length(minValues) == 1) {

if (all(is.na(toCensor))) {
data[, fieldName] <- NA
} else if (length(minValues) == 1) {
data[toCensor, fieldName] <- -minValues
} else {
data[toCensor, fieldName] <- -minValues[toCensor]
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-enforceMinCellValue.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
test_that("enforceMinCellValue works as expected", {
test_data <- data.frame(
A = c(1, 2, 3, NA, 0)
)
result <- enforceMinCellValue(test_data, "A", 2)
expect_equal(result$A, c(-2, 2, 3, NA, 0))
})


test_that("minCellValue handles bad inputs", {
x <- data.frame(a = c(0.1, 0.002))
res <- enforceMinCellValue(x, "a", NaN)
checkmate::expect_data_frame(res)
expect_true(all(is.na(res$a)))
})

0 comments on commit bd904fe

Please sign in to comment.