na.rm in dataframe

We have to use apply function to apply the function on the dataframe with na.rm function

Syntax: apply(dataframe, 2, function, na.rm )

where

  • dataframe is the input dataframe
  • function is to perform some operations like mean,min ,max etc
  • 2 represents column
  • na.rm is to remove NA values

Example 1: In this example, we are calculating the mean, sum, minimum, maximum, and standard deviation without NA in all columns

R




# create a dataframe with 3 columns
data = data.frame(column1 = c(1,2,NA,34),
                  column2 = c(NA,34,56,NA),
                  column3 = c(NA,NA,32,56))
  
# display
print(data)
  
# calculate mean including  NA values
apply(data, 2, mean , na.rm = FALSE)
  
# calculate sum including  NA values
apply(data, 2, sum , na.rm = FALSE)
  
# calculate min including  NA values
apply(data, 2, min, na.rm = FALSE)
  
# calculate max including  NA values
apply(data, 2, max , na.rm = FALSE)
  
  
# calculate standard deviation including 
# NA values
apply(data, 2, sd, na.rm = FALSE)


Output:

Example 2: Excluding NA values

R




# create a dataframe with 3 columns
data = data.frame(column1 = c(1,2,NA,34),
                  column2 = c(NA,34,56,NA),
                  column3 = c(NA,NA,32,56))
  
# display
print(data)
  
# calculate mean excluding  NA values
apply(data, 2, mean , na.rm = TRUE)
  
# calculate sum excluding    NA values
apply(data, 2, sum , na.rm = TRUE)
  
# calculate min excluding  NA values
apply(data, 2, min, na.rm = TRUE)
  
# calculate max excluding  NA values
apply(data, 2, max , na.rm = TRUE)
  
# calculate standard deviation excluding 
# NA values
apply(data, 2, sd, na.rm = TRUE)


Output:



How to Use na.rm in R?

In this article, we will discuss how to use na.rm in R Programming Language. na.rm in R is used to remove the NA values.

Similar Reads

na.rm in vector

When we perform any operation, we have to exclude NA values, otherwise, the result would be NA....

na.rm in dataframe

...