na.rm in vector

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

Syntax: function(vector,na.rm)

where

  • vector is input vector
  • na.rm is to remove NA values
  • function is to perform operation on vector like sum ,mean ,min ,max etc

Example 1: In this example, we are calculating the mean, sum, minimum, maximum, and standard deviation with  NA

R




# create a vector
data = c(1,2,3,NA,45,34,NA,NA,23)
  
# display
print(data)
  
# calculate mean including NA values
print(mean(data,na.rm=FALSE))
  
# calculate sum including NA values
print(sum (data,na.rm=FALSE))
  
# get minimum including NA values
print(min(data,na.rm=FALSE))
  
# get  maximum  including NA values
print(max(data,na.rm=FALSE))
  
# calculate standard deviation including
# NA values
print(sd (data,na.rm=FALSE))


Output:

[1]  1  2  3 NA 45 34 NA NA 23
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA

Example 2: In this example, we are calculating the mean, sum, minimum, maximum, and standard deviation without NA

R




# create a vector
data = c(1,2,3,NA,45,34,NA,NA,23)
  
# display
print(data)
  
# calculate mean excluding NA values
print(mean(data,na.rm=TRUE))
  
# calculate sum excluding NA values
print(sum (data,na.rm=TRUE))
  
# get minimum excluding  NA values
print(min(data,na.rm=TRUE))
  
# get  maximum  excluding  NA values
print(max(data,na.rm=TRUE))
  
# calculate standard deviation excluding 
# NA values
print(sd (data,na.rm=TRUE))


Output:

[1]  1  2  3 NA 45 34 NA NA 23
[1] 18
[1] 108
[1] 1
[1] 45
[1] 18.86796

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

...