Open In App

How to Use “Is Not NA” in R?

Last Updated : 19 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to use Is Not NA in R Programming Language.

NA is a value that is not a number. The is.na() method is used to check whether the given value is NA or not, we have to use the function for this. Inorder to use is NOT  NA, then we have to add the “!” operator to the is.na() function

Syntax:

!is.na(data)

where, data can be a vector/list, etc

Is Not NA in Vector

Here we can use this filter to get the values excluding NA values.

Syntax:

vector[!is.na(vector)]

where, vector is the input vector

Example:

R




# create a vector
vector1 = c(1, 2, 3, NA, 34, 56, 78, NA, NA, 34, NA)
  
# display vector
print(vector1)
  
# remove NA values using Not NA function
print(vector1[!is.na(vector1)])


Output:

[1]  1  2  3 NA 34 56 78 NA NA 34 NA
[1]  1  2  3 34 56 78 34

Is Not NA in dataframe in a single column

If we want to exclude NA values in dataframe columns, then we can use the dataframe similarly to the vector.

Syntax:

dataframe[!(is.na(dataframe$column_name)), ]

where

  • dataframe  is the input dataframe
  • column_name is the column to remove NA values

R




# create a dataframe
data = data.frame(marks1=c(NA, 34, 56, 78), 
                  marks2=c(45, 67, NA, NA))
  
# display
print(data)
  
# remove NA values using Not NA 
# function in marks1 column
print(data[!(is.na(data$marks1)), ])
  
# remove NA values using Not NA 
# function in marks2 column
print(data[!(is.na(data$marks2)), ])


Output:

Is Not NA in dataframe in multiple columns

Here we can filter in multiple columns using & operator.

dataframe[!(is.na(dataframe$column1)) & !(is.na(dataframe$column2)),]

Example:

R




# create a dataframe
data = data.frame(marks1=c(NA, 34, 56, 78), 
                  marks2=c(45, 67, NA, NA))
  
# display
print(data)
  
# remove NA values using Not NA function
# in marks1 and marks2 column
print(data[!(is.na(data$marks1)) & !(is.na(data$marks2)), ])


Output:

Remove all NA

Here we are going to remove NA’s in the entire dataframe by using na.omit() function

Syntax:

na.omit(dataframe)

Example:

R




# create a dataframe
data = data.frame(marks1=c(NA, 34, 56, 78),
                  marks2=c(45, 67, NA, NA))
  
# display
print(data)
  
# remove NA values using Not NA 
# function in entire dataframe
print(na.omit(data))


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads