View source: R/convert_to_na.R
convert_to_na | R Documentation |
Convert non-missing values in a variable into missing values.
convert_to_na(x, ...)
## S3 method for class 'numeric'
convert_to_na(x, na = NULL, verbose = TRUE, ...)
## S3 method for class 'factor'
convert_to_na(x, na = NULL, drop_levels = FALSE, verbose = TRUE, ...)
## S3 method for class 'data.frame'
convert_to_na(
x,
select = NULL,
exclude = NULL,
na = NULL,
drop_levels = FALSE,
ignore_case = FALSE,
regex = FALSE,
verbose = TRUE,
...
)
x |
A vector, factor or a data frame. |
... |
Not used. |
na |
Numeric, character vector or logical (or a list of numeric, character
vectors or logicals) with values that should be converted to |
verbose |
Toggle warnings. |
drop_levels |
Logical, for factors, when specific levels are replaced
by |
select |
Variables that will be included when performing the required tasks. Can be either
If |
exclude |
See |
ignore_case |
Logical, if |
regex |
Logical, if |
x
, where all values in na
are converted to NA
.
x <- sample(1:6, size = 30, replace = TRUE)
x
# values 4 and 5 to NA
convert_to_na(x, na = 4:5)
# data frames
set.seed(123)
x <- data.frame(
a = sample(1:6, size = 20, replace = TRUE),
b = sample(letters[1:6], size = 20, replace = TRUE),
c = sample(c(30:33, 99), size = 20, replace = TRUE)
)
# for all numerics, convert 5 to NA. Character/factor will be ignored.
convert_to_na(x, na = 5)
# for numerics, 5 to NA, for character/factor, "f" to NA
convert_to_na(x, na = list(6, "f"))
# select specific variables
convert_to_na(x, select = c("a", "b"), na = list(6, "f"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.