How to Use "Is Not NA" in R?
Last Updated :
19 Dec, 2021
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:
Similar Reads
How to Use is.na in R? In this article we will discuss how to use is.na in R programming language. is.na is used to check NA values present in the given data and return TRUE if the value is NA, otherwise FALSE Syntax: is.na(data) where, data is a vector/dataframe is.na() can be used with other methods to add more meaning
2 min read
How to Use na.omit in R? What are missing values?In data analysis, missing values refer to the absence of data for a particular variable or observation. These missing values are typically represented by a special symbol or code, often denoted as "NA" (Not Available) in R and many other programming languages. na.omit() funct
2 min read
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. 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 vectorna.rm is
3 min read
How to Use âNOT INâ Operator in R? In this article, we will discuss NOT IN Operator in R Programming Language. NOT IN Operator is used to check whether the element in present or not. The symbol used for IN operator is "%in%". For NOT IN operator we have to add " ! " operator before that , so the symbol for NOT IN operator is "! %in%"
2 min read
How to Use Nrow Function in R? In this article, we will discuss how to use Nrow function in R Programming Language. This function is used in the dataframe or the matrix to get the number of rows. Syntax: nrow(data) where, data can be a dataframe or a matrix. Example 1: Count Rows in Data Frame In this example, we are going to cou
2 min read