Description Usage Arguments Details Value See Also Examples
Replaces NA
values with explicit values.
1 2 3 | na_replace(x, .na, ...)
na.replace(x, .na)
|
x |
vector in which |
.na |
value or object used to replace |
na_replace
replaces missing values in x
by .na
. Replacement is both
class/type and length safe meaning the result is guaranteed to be the
same class/type and length of x
regardless of the value of .na
.
Param: x
If x
is categorical (e.g. character or factor), .na
is optional.
The default is "(NA)" and can be set with
options( NA_explicit_ = new_value )
. It can also be
referenced directly with NA_explicit_.
If x
is a factor, unique values of .na
not in already present in
levels(x)
will be added. They are appended silently unless
getOption('verbose')==TRUE
in which a message reports the added levels.
Param: .na
.na
can be either a scalar, vector or function.
If a vector, .na
can be a length-1 scalar or length(x)-vector. If a
length-1 scalar all missing values are replaced by it. If a
length(x)-vector, missing values are replaced by the corresponding elements of
.na. Recycling values of
.na' is not allowed.
na.replace
is a alias for na_replace that is likely to be deprecated.
A vector with the class and length of x
.
NA
s in x
will be replaced by .na
. .na
is coerced as necessary.
na_explicit()
for a similar function that additionally handles recursive
and table-like objects
base::ifelse()
, base::replace()
forcats::fct_explicit_na
- which only handles factors
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # Integers and numerics
na_replace( c(1,NA,3,NA), 2 ) # 1 2 3 2
na_replace( c(1,NA,3,NA), 1:4 ) # 1 2 3 4
na_replace( c(1,NA,3,NA), letters[1:4] ) # "1" "b" "3" "d"
# Characters
lets <- letters[1:5]
lets[ c(2,4) ] <- NA
lets %>% na_replace
# Factors
fct <- as.factor( c( NA, letters[2:4], NA) )
fct
na_replace(fct, "z") # z b c d z -- level z added
na_replace(fct, letters[1:5] )
na_replace(fct)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.