What is the difference between na.omit and na.rm

r

#1

Hello,

Sometimes in R we use na.rm and sometimes na.omit(i have used during subsetting).What is the difference between the two.??


#2

The main difference is that na.rm is not a function. It is only an argument used by some functions. To test this enter na.rm into the console, then try na.omit.

If a function that accepts na.rm as an argument is called, use it. It will save time and storage space.

Use na.omit to quickly eliminate NA’s from a vector:

x <- c(NA, 1, NA, 20)
mean(x)
[1] NA

x2 <- na.omit(x)
x2
[1]  1 20
attr(,"na.action")
[1] 1 3
attr(,"class")
[1] "omit"

For some users, while the vector still behaves as a normal vector, the output structure is undesirable. The new object shows special attributes that make note of the position of the elements that were omitted, and the class “omit”. The hope for most users is to only see the non-NA values:

x2
[1] 1 20

For this, the sister function, complete.cases can help. It returns a logical vector as output. The non-NA values will be TRUE. Vectors can be subset by that type of output:

x2 <- x[complete.cases(x)]
x2
[1] 1 20

mean(x2)
[1] 10.5

The mean function accepts na.rm as an argument. So there is no need to create a new object without NA’s.

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