diff --git a/R/recode_into.r b/R/recode_into.r index 3cc845ff5..7cb4d8340 100644 --- a/R/recode_into.r +++ b/R/recode_into.r @@ -140,7 +140,8 @@ recode_into <- function(..., data = NULL, default = NA, overwrite = TRUE, verbos } # if user doesn't want to overwrite, remove already recoded indices if (!overwrite) { - index <- index[!already_exists] + index[which(index)[already_exists]] <- FALSE + # index <- index[which(index)[!already_exists]] } out[index] <- value } diff --git a/tests/testthat/test-recode_into.R b/tests/testthat/test-recode_into.R index 9fa896986..aaed73b34 100644 --- a/tests/testthat/test-recode_into.R +++ b/tests/testthat/test-recode_into.R @@ -19,6 +19,29 @@ test_that("recode_into, overwrite", { ), regex = "overwritten" ) + # validate results + x <- 1:10 + expect_silent({ + out <- recode_into( + x >= 3 & x <= 7 ~ 1, + x > 5 ~ 2, + default = 0, + verbose = FALSE + ) + }) + expect_identical(out, c(0, 0, 1, 1, 1, 2, 2, 2, 2, 2)) + + x <- 1:10 + expect_silent({ + out <- recode_into( + x >= 3 & x <= 7 ~ 1, + x > 5 ~ 2, + default = 0, + overwrite = FALSE, + verbose = FALSE + ) + }) + expect_identical(out, c(0, 0, 1, 1, 1, 1, 1, 2, 2, 2)) }) test_that("recode_into, don't overwrite", {