Skip to content

Commit

Permalink
fix wrong case number in warning (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Jun 27, 2023
1 parent df85e1d commit a14b606
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: datawizard
Title: Easy Data Wrangling and Statistical Transformations
Version: 0.8.0.2
Version: 0.8.0.3
Authors@R: c(
person("Indrajeet", "Patil", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ BUG FIXES
* Fixed issues in `data_write()` when writing labelled data into SPSS format
and vectors were of different type as value labels.

* Fixed issue in `recode_into()` with probably wrong case number printed in the
warning when several recode patterns match to one case.

# datawizard 0.8.0

BREAKING CHANGES
Expand Down
9 changes: 6 additions & 3 deletions R/recode_into.r
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,28 @@ recode_into <- function(..., data = NULL, default = NA, overwrite = TRUE, verbos
} else {
already_exists <- out[index] != default
}
# save indices of overwritten cases
overwritten_cases <- which(index)[already_exists]
# tell user...
if (any(already_exists) && verbose) {
if (overwrite) {
msg <- paste(
"Several recode patterns apply to the same cases.",
"Some of the already recoded cases will be overwritten with new values again",
sprintf("(e.g. pattern %i overwrites the former recode of case %i).", i, which(already_exists)[1])
sprintf("(e.g. pattern %i overwrites the former recode of case %i).", i, overwritten_cases[1])
)
} else {
msg <- paste(
"Several recode patterns apply to the same cases.",
"Some of the already recoded cases will not be altered by later recode patterns.",
sprintf("(e.g. pattern %i also matches the former recode of case %i).", i, which(already_exists)[1])
sprintf("(e.g. pattern %i also matches the former recode of case %i).", i, overwritten_cases[1])
)
}
insight::format_warning(msg, "Please check if this is intentional!")
}
# if user doesn't want to overwrite, remove already recoded indices
if (!overwrite) {
index[which(index)[already_exists]] <- FALSE
index[overwritten_cases] <- FALSE
}
out[index] <- value
}
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-recode_into.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ test_that("recode_into, overwrite", {
)
})
expect_identical(out, c(0, 0, 1, 1, 1, 2, 2, 2, 2, 2))
expect_warning(
recode_into(
x >= 3 & x <= 7 ~ 1,
x > 5 ~ 2,
default = 0
),
regex = "case 6"
)

x <- 1:10
expect_silent({
Expand All @@ -42,6 +50,15 @@ test_that("recode_into, overwrite", {
)
})
expect_identical(out, c(0, 0, 1, 1, 1, 1, 1, 2, 2, 2))
expect_warning(
recode_into(
x >= 3 & x <= 7 ~ 1,
x > 5 ~ 2,
default = 0,
overwrite = FALSE
),
regex = "case 6"
)
})

test_that("recode_into, don't overwrite", {
Expand Down

0 comments on commit a14b606

Please sign in to comment.