Skip to content

Commit 4d17b3d

Browse files
committed
var_case
1 parent 0977008 commit 4d17b3d

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

R/var_case.R

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33
#' @param newvr name of the new logical variable to be created
44
#' @param vr factor variable
55
#' @param cases one or more levels of `vr` that are converted to `TRUE`. All other levels are converted to `FALSE`.
6+
#' @param retain_na for the observations where `vr` is `NA`, should `newvr` be `NA` as well?
67
#'
78
#' @return Survey object
89
#' @family variables
910
#' @export
1011
#'
1112
#' @examples
1213
#' set_survey(namcs2019sv)
14+
#'
1315
#' var_case("Preventive care visits", "MAJOR", "Preventive care")
1416
#' tab("Preventive care visits")
17+
#'
1518
#' var_case("Surgery-related visits"
1619
#' , "MAJOR"
1720
#' , c("Pre-surgery", "Post-surgery"))
1821
#' tab("Surgery-related visits")
19-
var_case = function(newvr, vr, cases) {
22+
#'
23+
#' var_case("Non-primary"
24+
#' , "SPECCAT.bad"
25+
#' , c("Surgical care specialty", "Medical care specialty"))
26+
#' tab("Non-primary")
27+
#' tab("Non-primary", drop_na = TRUE)
28+
var_case = function(newvr, vr, cases, retain_na = TRUE) {
2029
design = .load_survey()
2130
nm = names(design$variables)
2231
assert_that(vr %in% nm, msg = paste("Variable", vr, "not in the data."))
@@ -30,6 +39,10 @@ var_case = function(newvr, vr, cases) {
3039
}
3140

3241
design$variables[,newvr] = FALSE
42+
if (retain_na) {
43+
idx = which(is.na(design$variables[,vr]))
44+
design$variables[idx, newvr] = NA
45+
}
3346
idx = which(design$variables[,vr] %in% cases)
3447
design$variables[idx, newvr] = TRUE
3548

docs/pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pkgdown_sha: ~
44
articles:
55
Example-National-Ambulatory-Medical-Care-Survey-NAMCS-tables: Example-National-Ambulatory-Medical-Care-Survey-NAMCS-tables.html
66
surveytable: surveytable.html
7-
last_built: 2024-03-29T15:28Z
7+
last_built: 2024-04-04T14:42Z
88
urls:
99
reference: https://cdcgov.github.io/surveytable/reference
1010
article: https://cdcgov.github.io/surveytable/articles

docs/reference/var_case.html

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/search.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

man/var_case.Rd

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)