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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Nov, 2023
Like Article
Save Article