NA items in R data

The NA item is a special object and represents “Not Available”. Sometimes this is because data were genuinely not collected but often it is because you have unequal columns and your data frame is padded out with NA to make the short columns longer. The na.rm = TRUE instruction can be used to strip out NA items before some commands, for example:

> x
[1] 2 4 3 6 2 8 NA NA

> mean(x)
[1] NA

> mean(x, na.rm = TRUE)
[1] 4.166667

However, this does not always work:

> length(x, na.rm = TRUE)
Error in length(x, na.rm = TRUE) :
2 arguments passed to ‘length’ which requires 1

In this case the na.omit() command can be used to strip out the NA items:

> length(na.omit(x))
[1] 6
Comments are closed.