Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recode_into can skip overwriting former recodes #438

Merged
merged 5 commits into from
Jun 27, 2023
Merged

Conversation

strengejacke
Copy link
Member

recode_into() by defaults overwrite former recodings when multiple recode patterns apply. This PR adds an overwrite argument that allows users to skip this behaviour, preserving already recoded cases and not overwrite them.

library(datawizard)
x <- 1:30

recode_into(
  x > 1 ~ "a",
  x > 10 & x <= 15 ~ "b",
  default = "c"
)
#> Warning: Several recode patterns apply to the same cases. Some of the already
#>   recoded cases will be overwritten with new values again (e.g. pattern 2
#>   overwrites the former recode of case 1).
#>   Please check if this is intentional!
#>  [1] "c" "a" "a" "a" "a" "a" "a" "a" "a" "a" "b" "b" "b" "b" "b" "a" "a" "a" "a"
#> [20] "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"

recode_into(
  x > 1 ~ "a",
  x > 10 & x <= 15 ~ "b",
  default = "c",
  overwrite = FALSE
)
#> Warning: Several recode patterns apply to the same cases. Some of the already
#>   recoded cases will not be altered by later recode patterns. (e.g.
#>   pattern 2 also matches the former recode of case 1).
#>   Please check if this is intentional!
#>  [1] "c" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"
#> [20] "a" "a" "a" "a" "a" "a" "a" "a" "a" "a" "a"

@codecov-commenter
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (main@3cdc3fa). Click here to learn what that means.
The diff coverage is 100.00%.

❗ Current head 39cfade differs from pull request most recent head 6748578. Consider uploading reports for the commit 6748578 to get more accurate results

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

@@           Coverage Diff           @@
##             main     #438   +/-   ##
=======================================
  Coverage        ?   88.51%           
=======================================
  Files           ?       68           
  Lines           ?     5033           
  Branches        ?        0           
=======================================
  Hits            ?     4455           
  Misses          ?      578           
  Partials        ?        0           
Impacted Files Coverage Δ
R/recode_into.r 100.00% <100.00%> (ø)

@strengejacke
Copy link
Member Author

library(datawizard)

x <- 1:10

# default behaviour, second patterns overwrites
# formerly recoded values > 5 into 2
recode_into(
  x >= 3 & x <= 7 ~ 1,
  x > 5 ~ 2,
  default = 0,
  overwrite = TRUE,
  verbose = FALSE
)
#>  [1] 0 0 1 1 1 2 2 2 2 2

# behaviour when "overwrite = FALSE", second patterns does not overwrite
# formerly recoded values from 3 to 7, and only changes values > 7
recode_into(
  x >= 3 & x <= 7 ~ 1,
  x > 5 ~ 2,
  default = 0,
  overwrite = FALSE,
  verbose = FALSE
)
#>  [1] 0 0 1 1 1 1 1 2 2 2

Created on 2023-06-27 with reprex v2.0.2

@strengejacke strengejacke merged commit 0be859c into main Jun 27, 2023
@strengejacke strengejacke deleted the dont_overwrite branch June 27, 2023 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants