How to Use na.omit in R?
Last Updated :
28 Nov, 2023
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() function in R
The na.omit()
function in R Programming Language is used to remove missing values (NAs) from a data frame, matrix, or vector. The name “na.omit” stands for “omit NAs.” This function is particularly useful when working with datasets that contain missing values, and you want to exclude observations with missing data from your analysis.
Syntax:
na.omit(data)
Parameter:
data: Set of specified values of a data frame, matrix, or vector.
Returns: Range of values after NA omission.
Removing Missing Values from Vector
R
vector <- c (1, 2, NA , 4, 5)
vector
cleaned_vector <- na.omit (vector)
cleaned_vector
|
Output:
[1] 1 2 NA 4 5
[1] 1 2 4 5
Removing Missing Values from matrix
R
mat<- c ( NA ,1,2, NA ,3,4, NA ,5,6, NA ,7,8)
var<- matrix (mat,3,4)
var
na.omit (var)
|
Output:
[,1] [,2] [,3] [,4]
[1,] NA NA NA NA
[2,] 1 3 5 7
[3,] 2 4 6 8
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
Removing Missing Values from Data Frames
R
data <- data.frame (
ID = c (1, 2, 3, 4),
Value = c (5, NA , 7, 8)
)
data
cleaned_data <- na.omit (data)
print (cleaned_data)
|
Output:
ID Value
1 1 5
2 2 NA
3 3 7
4 4 8
ID Value
1 1 5
3 3 7
4 4 8
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...