[R] Using na.omit() on the output of na.omit()
Shu Fai Cheung
@hu|@|@cheung @end|ng |rom gm@||@com
Fri Mar 27 15:13:48 CET 2026
Hi All,
I noticed a behavior of na.omit() that I am not sure is intended.
This is adapted from the example of na.omit()
> DF <- data.frame(x = c(1, 2, 3), y = c(NA, 10, 1))
> DF_omitted <- na.omit(DF)
> attr(DF_omitted, "na.action")
1
1
attr(,"class")
[1] "omit"
> DF_omitted2 <- na.omit(DF_omitted)
> attr(DF_omitted2, "na.action")
1
1
attr(,"class")
[1] "omit"
In the first call to na.omit(), the output, DF_omitted, correctly has only
two rows, with Row 1 removed, and 'na.action' stores the removed row number.
In the second call of na.omit(), no cases are removed because DF_omitted
has no missing data. However, the attribute 'na.action' is retained,
indicating that Row 1 was removed.
To my understanding of na.omit(), this occurred because, in the second call
of na.omit(), no rows were omitted, and so the original object was
returned, along with the attribute 'na.action' from the first call.
>From the "perspective" of the second call, no rows were omitted.
Should 'na.action'
be NULL (i.e., not set), as in the following example?
> DF2 <- data.frame(x = c(1, 2, 3), y = c(3, 10, 1))
> DF2_omitted <- na.omit(DF2)
> attr(DF2_omitted, "na.action")
NULL
Or is this behavior of na.omit() intended?
Regards,
Shu Fai
[[alternative HTML version deleted]]
More information about the R-help
mailing list