Skip to content

Commit

Permalink
fix, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 27, 2023
1 parent 6748578 commit 06c4270
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion R/recode_into.r
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-recode_into.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit 06c4270

Please sign in to comment.