You can use addNA()
.
x <- c(1, 1, 2, 2, 3, NA)
addNA(x)
# [1] 1 1 2 2 3 <NA>
# Levels: 1 2 3 <NA>
This is basically a convenience function for factoring with exclude = NULL
. From help(factor)
–
addNA
modifies a factor by turningNA
into an extra level (so thatNA
values are counted in tables, for instance).
So another reason this is nice is because if you already have a factor f
, you can use addNA()
to quickly add NA
as a factor level without changing f
. As mentioned in the documentation, this is handy for tables. It also reads nicely.