Getting rid of missing data
An alternative way of using the na.rm
argument in R functions is removing NA
from the dataset before passing that to the analysis functions. This means that we are removing the missing values from the dataset permanently, so that they won't cause any problems at later stages in the analysis. For this, we could use either the na.omit
or the na.exclude
functions:
> na.omit(c(1:5, NA)) [1] 1 2 3 4 5 attr(,"na.action") [1] 6 attr(,"class") [1] "omit" > na.exclude(c(1:5, NA)) [1] 1 2 3 4 5 attr(,"na.action") [1] 6 attr(,"class") [1] "exclude"
The only difference between these two functions is the class of the na.action
attribute of the returned R object, which are omit
and exclude
respectively. This minor difference is only important when modelling. The na.exclude
function returns NA
for residuals and predictions, while na.omit
suppresses those elements of the vector:
> x <- rnorm(10); y <- rnorm(10) > x[1] <- NA; y[2] <- NA > exclude <...