How To Use na.rm In R

If you have ever done any research involving real-world measurements, then you know that the data is not always neat and tidy. In a lab, you can control the quality of the data, but the real world does not work so nicely. Sometimes, things beyond your control can cause gaps in the data.

Deal with missing data in r

There are several ways to deal with missing data in r. One way is the function involves simply detecting it. Another the na.omit() function deletes any rows in the dataframe containing missing data in R missing data is designated by NA so that it can be detected easily. It is accepted by data.frame() without difficulty. While the cbind() function will accept data containing NA, it does produce a warning. one way of dealing with missing data with dataframe functions is through the na.rm logical perimeter.

Remove NA values in r

Because the NA value is a placeholder and not an actual numeric value, it cannot be included in calculations. So, somehow it needs to be removed from the calculations to get a meaningful value. If you include the NA value in a calculation it will result in an NA value. While this may be okay sometimes in other cases you need a number. The two remove NA values in r is by the na.omit() function that deletes the entire row, and the na.rm logical perimeter which tells the function to skip that value.

What does na.rm mean in r?

When using a dataframe function na.rm in r refers to the logical parameter that tells the function whether or not to remove NA values from the calculation. It literally means NA remove. It is neither a function nor an operation. It is simply a parameter used by several dataframe functions. They include colSums(), rowSums(), colMeans() and rowMeans(). When na.rm is TRUE, the function skips over any NA values. However, when na.rm is FALSE, then it returns NA from the calculation being done on the entire row or column.

Examples of na.rm in r

To start our examples, we need to set up a dataframe to work from.

# na.rm in r example 
> x=data.frame(a=c(2,3,5,8),b=c(3,8,NA,5),c=c(10,4,6,11))
 > x
   a  b  c
 1 2  3 10
 2 3  8  4
 3 5 NA  6
 4 8  5 11

Note the NA in row 3 column b, this shall be the missing data set for these examples.

# na.rm in r
> colMeans(x, na.rm = TRUE, dims = 1)
        a        b        c 
 4.500000 5.333333 7.750000
 > rowSums(x, na.rm = FALSE, dims = 1)
 [1] 15 15 NA 24

> rowSums(x, na.rm = TRUE, dims = 1)
 [1] 15 15 11 24

The second and third examples are identical except that in the first one na.rm = FALSE, and na.rm = TRUE in the other. That makes all the difference.

Dealing with missing data from a data set is critical to proper data science. Because R makes dealing with this missing data so easy is another reason it is so often used in statistical analysis.