Method 3 : Using omit() method

omit() method is used to remove the NA values directly by resulting in the non-NA values and omitted NA values indexes.

Syntax: 

na.omit(vector)

where the vector is the input vector

Return type:

  • Returns the non-NA values
  • Returns the indexes of NA values which are removed from the vector

Note: Indexing starts with 1

Example: R program to consider a vector and remove NA values

R




# create a vector with integers along with NA
a=c(1,2,NA,4,5,NA,4,5,6,NA)
 
# display
print(a)
 
print("_______________________")
 
# remove NA using omit() function
a=na.omit(a)
 
# display vector
print(a)


Output:

[1]  1  2 NA  4  5 NA  4  5  6 NA
[1] "_______________________"
[1] 1 2 4 5 4 5 6
attr(,"na.action")
[1]  3  6 10
attr(,"class")
[1] "omit"


Remove NA Values from Vector in R

In this article, we are going to discuss how to remove NA values from the vector.

Similar Reads

Method 1: Using is.na()

We can remove those NA values from the vector by using is.na(). is.na() is used to get the na values based on the vector index. !is.na() will get the values except na....

Method 2: Using na.rm

...

Method 3 : Using omit() method

we can also remove na values by computing the sum, mean, variance....