6.4 Missing (NA) values
In R, missing data are coded as NA. In real datasets, NA values turn up all the time. Unfortunately, most descriptive statistics functions will freak out if there is a missing (NA) value in the data. For example, the following code will return NA as a result because there is an NA value in the data vector:
Thankfully, there’s a way we can work around this. To tell a descriptive statistic function to ignore missing (NA) values, include the argument na.rm = TRUE
in the function. This argument explicitly tells the function to ignore NA values. Let’s try calculating the mean of the vector a
again, this time with the additionalna.rm = TRUE
argument:
Now, the function ignored the NA value and returned the mean of the remaining data. While this may seem trivial now (why did we include an NA value in the vector if we wanted to ignore it?!), it will be become very important when we apply the function to real data which, very often, contains missing values.