-
Notifications
You must be signed in to change notification settings - Fork 2
/
recodeNA2missing.R
54 lines (51 loc) · 2.29 KB
/
recodeNA2missing.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#' Recode \code{NAs} to Missing.
#'
#' Recode \code{NAs} in multiple variables in a \code{GADSdat} to a numeric value with a value label and a missing tag.
#'
#' The value label and missing tag are only added to variables which contain \code{NAs} and which have been recoded.
#' If a variable has an existing value label for \code{value}, the existing value label is overwritten and a missing tag is added.
#' A corresponding warning is issued.
#'
#'
#'@param GADSdat A \code{GADSdat} object.
#'@param recodeVars Character vector of variable names which should be recoded.
#'@param value Which value should \code{NAs} be recoded to?
#'@param valLabel Which value label should \code{value} be assigned?
#'
#'@return Returns the recoded \code{GADSdat}.
#'
#'@examples
#' # create example GADS
#' dat <- data.frame(ID = 1:4, age = c(NA, 18, 21, 23),
#' siblings = c(0, 2, NA, NA))
#' gads <- import_DF(dat)
#'
#' # recode NAs
#' gads2 <- recodeNA2missing(gads)
#'@export
recodeNA2missing <- function(GADSdat, recodeVars = namesGADS(GADSdat), value = -99, valLabel = "missing") {
UseMethod("recodeNA2missing")
}
#'@export
recodeNA2missing.GADSdat <- function(GADSdat, recodeVars = namesGADS(GADSdat), value = -99, valLabel = "missing") {
check_GADSdat(GADSdat)
if(!is.character(recodeVars) || length(recodeVars) < 1) {
stop("'recodeVars' needs to be character vector of at least length 1.")
}
check_vars_in_GADSdat(GADSdat, vars = recodeVars, argName = "recodeVars")
check_numericArgument(value, argName = "value")
check_characterArgument(valLabel, argName = "valLabel")
for(recodeVar in recodeVars) {
if(any(is.na(GADSdat$dat[[recodeVar]]))) { # only variables with actual NAs should receive new meta data
## check for valueLabel conflicts
labeled_values <- extractMeta(GADSdat, recodeVar)
if(!all(is.na(labeled_values$value)) && value %in% labeled_values$value) {
warning("'value' is already labeled for the following variable in 'recodeVars': ", recodeVar)
}
GADSdat <- recodeGADS(GADSdat, varName = recodeVar, oldValues = NA, newValues = value)
GADSdat <- changeValLabels(GADSdat, varName = recodeVar, value = value, valLabel = valLabel)
GADSdat <- changeMissings(GADSdat, varName = recodeVar, value = value, missings = "miss")
}
}
GADSdat
}