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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#' @title Replace missing values in a variable or a data frame.
#' @name convert_na_to
#'
#' @description
#' Replace missing values in a variable or a data frame.
#'
#' @param x A numeric, factor, or character vector, or a data frame.
#' @param replacement Numeric or character value that will be used to
#' replace `NA`.
#' @param verbose Toggle warnings.
#' @param ... Not used.
#'
#' @inheritSection center Selection of variables - the `select` argument
#'
#' @return
#' `x`, where `NA` values are replaced by `replacement`.
#'
#' @examples
#' # Convert NA to 0 in a numeric vector
#' convert_na_to(
#' c(9, 3, NA, 2, 3, 1, NA, 8),
#' replacement = 0
#' )
#'
#' # Convert NA to "missing" in a character vector
#' convert_na_to(
#' c("a", NA, "d", "z", NA, "t"),
#' replacement = "missing"
#' )
#'
#' ### For data frames
#'
#' test_df <- data.frame(
#' x = c(1, 2, NA),
#' x2 = c(4, 5, NA),
#' y = c("a", "b", NA)
#' )
#'
#' # Convert all NA to 0 in numeric variables, and all NA to "missing" in
#' # character variables
#' convert_na_to(
#' test_df,
#' replace_num = 0,
#' replace_char = "missing"
#' )
#'
#' # Convert a specific variable in the data frame
#' convert_na_to(
#' test_df,
#' replace_num = 0,
#' replace_char = "missing",
#' select = "x"
#' )
#'
#' # Convert all variables starting with "x"
#' convert_na_to(
#' test_df,
#' replace_num = 0,
#' replace_char = "missing",
#' select = starts_with("x")
#' )
#'
#' # Convert NA to 1 in variable 'x2' and to 0 in all other numeric
#' # variables
#' convert_na_to(
#' test_df,
#' replace_num = 0,
#' select = list(x2 = 1)
#' )
#'
#' @export
convert_na_to <- function(x, ...) {
UseMethod("convert_na_to")
}
#' @export
convert_na_to.default <- function(x, verbose = TRUE, ...) {
if (isTRUE(verbose)) {
insight::format_alert(
sprintf(
"Converting missing values (`NA`) into regular values currently not possible for variables of class `%s`.",
class(x)[1]
)
)
}
x
}
#' @rdname convert_na_to
#' @export
convert_na_to.numeric <- function(x, replacement = NULL, verbose = TRUE, ...) {
if (insight::is_empty_object(replacement) || !is.numeric(replacement)) {
if (isTRUE(verbose)) {
insight::format_warning("`replacement` needs to be a numeric vector.")
}
} else if (length(replacement) > 1) {
if (isTRUE(verbose)) {
insight::format_warning("`replacement` needs to be of length one.")
}
} else {
x[is.na(x)] <- replacement
}
x
}
#' @export
convert_na_to.factor <- function(x, replacement = NULL, verbose = TRUE, ...) {
if (insight::is_empty_object(replacement) || length(replacement) > 1) {
if (isTRUE(verbose)) {
insight::format_warning("`replacement` needs to be of length one.")
}
} else {
x <- addNA(x)
levels(x) <- c(levels(x), replacement)
x[is.na(x)] <- replacement
}
x
}
#' @rdname convert_na_to
#' @export
convert_na_to.character <- function(x, replacement = NULL, verbose = TRUE, ...) {
if (insight::is_empty_object(replacement) || !is.character(replacement) && !is.numeric(replacement)) {
if (isTRUE(verbose)) {
insight::format_warning(
"`replacement` needs to be a character or numeric vector."
)
}
} else if (length(replacement) > 1) {
if (isTRUE(verbose)) {
insight::format_warning("`replacement` needs to be of length one.")
}
} else {
x[is.na(x)] <- as.character(replacement)
}
x
}
#' @param replace_num Value to replace `NA` when variable is of type numeric.
#' @param replace_char Value to replace `NA` when variable is of type character.
#' @param replace_fac Value to replace `NA` when variable is of type factor.
#' @inheritParams extract_column_names
#'
#' @rdname convert_na_to
#' @export
convert_na_to.data.frame <- function(x,
select = NULL,
exclude = NULL,
replacement = NULL,
replace_num = replacement,
replace_char = replacement,
replace_fac = replacement,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...) {
my_data <- x
select_nse <- .select_nse(
select,
data = my_data,
exclude = exclude,
ignore_case,
regex = regex,
verbose = verbose
)
# default
lookup <- lapply(x, function(y) {
if (is.numeric(y)) {
replace_num
} else if (is.character(y)) {
replace_char
} else if (is.factor(y)) {
replace_fac
}
})
# override for specific vars
try_eval <- try(eval(select), silent = TRUE)
select_is_list <- !inherits(try_eval, "try-error") && is.list(select)
if (select_is_list) {
for (i in select_nse) {
lookup[[i]] <- select[[i]]
}
} else {
lookup <- lookup[names(lookup) %in% select_nse]
}
lookup <- Filter(Negate(is.null), lookup)
for (i in names(lookup)) {
x[[i]] <- convert_na_to(x[[i]], replacement = lookup[[i]], verbose = verbose)
}
x
}
|