Note that this is done with the OP's data before the call to addNA()
.
It is instructive to see what addNA()
does with this data.
> head(df1$var1)
[1] <NA> def ghi jkl <NA> def
Levels: abc def ghi jkl
> levels(df1$var1)
[1] "abc" "def" "ghi" "jkl"
> head(addNA(df1$var1))
[1] <NA> def ghi jkl <NA> def
Levels: abc def ghi jkl <NA>
> levels(addNA(df1$var1))
[1] "abc" "def" "ghi" "jkl" NA
addNA
is altering the levels of the factor such that missing-ness (NA
) is a level where by default R ignores it as what level the NA
values take is, of course, missing. It is also stripping out the NA
information - in a sense it is no longer unknown but part of a category "missing".
To look at the help for addNA
us ?addNA
.
If we look at the definition of addNA
we see that all it is doing is altering the levels
of the factor, not changing the data any:
> addNA
function (x, ifany = FALSE)
{
if (!is.factor(x))
x <- factor(x)
if (ifany & !any(is.na(x)))
return(x)
ll <- levels(x)
if (!any(is.na(ll)))
ll <- c(ll, NA)
factor(x, levels = ll, exclude = NULL)
}
Note that it doesn't otherwise change the data - the NA
are still there in the factor. We can replicate most of the behaviour of addNA
via:
with(df1, factor(var1, levels = c(levels(var1), NA), exclude = NULL))
> head(with(df1, factor(var1, levels = c(levels(var1), NA), exclude = NULL)))
[1] <NA> def ghi jkl <NA> def
Levels: abc def ghi jkl <NA>
However because NA
is now a level, those entries are not indicated as being missing via is.na()
That explains the second comparison you do not working (where you use is.na()
).
The only nicety you get from addNA
is that it doesn't add NA
as a level if it already exists as one. Also, via the ifany
you can stop it adding NA
as a level if there are no NA
s in the data.
Where you are going wrong is attempting to compare an NA
with something using the usual comparison methods (except your second example). If we don't know what value and NA
observation takes, how can we compare it with something? Well, we can't, other than with the internal representation of NA
. This is what is done by the is.na()
function:
> with(df1, head(is.na(var1), 10))
[1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
Hence I would do (without using addNA
at all)
df1 <- transform(df1, isNaCol = is.na(var1))
> head(df1)
id y var1 var2 var3 isNaCol
1 1 1 <NA> ab c abc TRUE
2 2 0 def ghi ghi FALSE
3 3 0 ghi jkl nop FALSE
4 4 0 jkl def xyz FALSE
5 5 0 <NA> ab c abc TRUE
6 6 1 def ghi ghi FALSE
If you want that as a 1
, 0
, variable, just add as.numeric()
as in
df1 <- transform(df1, isNaCol = as.numeric(is.na(var1)))
Where I think you are really going wrong is in wanting to attach an NA
level to the factor. I see addNA()
as a convenience function for use in things like table()
, and even that has arguments to not need the prior use of addNA()
, e.g.:
> with(df1, table(var1, useNA = "ifany"))
var1
abc def ghi jkl <NA>
0 50 50 50 50
any(is.na(as.character(df1$var1)))
returnsTRUE
... But I'm not sure why it isn't working directly...x=factor('a'); x[1]=NA; addNA(x); is.na(x)
returnsTRUE
is it should... – Cordaliax[1]=NA
, you're setting the level index to NA, not the value. Seeas.numeric(x)
vsas.numeric(df1$var1)
.is.na
looks at the level indexes. – Olnay