Complete Cases in R with Examples
In this article, we will discuss what is complete.cases() Function and where it is used in R Programming Language.
What is complete.cases() Function
complete.cases()
function in R Programming Language is used to return a logical vector with cases that are complete, i.e., no missing value. This function is especially handy when dealing with datasets that may have missing data.
Syntax:
complete.cases(x)
Parameters:x: Object
Perform complete.cases() Function on Vector
R
vec <- c (1, 2, 3, 4, NA , 3)
complete.cases (vec)
vec1 <- vec[ complete.cases (vec)]
vec1
|
Output:
[1] TRUE TRUE TRUE TRUE FALSE TRUE
[1] 1 2 3 4 3
Perform complete.cases() Function on Matrix
R
matrix_data <- matrix ( c (1, 2, NA , 4, 5, 6, 7, 8, 9),3,3)
complete_rows <- complete.cases (matrix_data)
print (matrix_data)
complete_matrix <- matrix_data[complete_rows, , drop = FALSE ]
print (complete_matrix)
|
Output:
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] NA 6 9
Print the matrix with complete cases
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
Perform complete.cases() Function on Data frame
R
data_frame <- data.frame (
StudentID = c (101, 102, 103,104),
ExamScore = c (85, NA , 92,68),
Attendance = c (90, 75, NA ,80),
Grade = c ( "A" , "B" , "C" , "D" )
)
print (data_frame)
complete_rows <- complete.cases (data_frame)
complete_data_frame <- data_frame[complete_rows, , drop = FALSE ]
print (complete_data_frame)
|
Output:
StudentID ExamScore Attendance Grade
1 101 85 90 A
2 102 NA 75 B
3 103 92 NA C
4 104 68 80 D
complete_data_frame
StudentID ExamScore Attendance Grade
1 101 85 90 A
4 104 68 80 D
Rows containing NA in specific columns of a data frame should be removed
R
data_frame <- data.frame (
StudentID = c (101, 102, 103,104),
ExamScore = c (85, NA , 92,68),
Attendance = c (90, 75, NA ,80),
Grade = c ( "A" , "B" , "C" , "D" )
)
print (data_frame)
complete_rows <- complete.cases (data_frame[ , 'ExamScore' ])
complete_data_frame <- data_frame[complete_rows, , drop = FALSE ]
print (complete_data_frame)
|
Output:
StudentID ExamScore Attendance Grade
1 101 85 90 A
2 102 NA 75 B
3 103 92 NA C
4 104 68 80 D
StudentID ExamScore Attendance Grade
1 101 85 90 A
3 103 92 NA C
4 104 68 80 D
Here we remove the only Rows containing NA in specific columns of a data frame.
Last Updated :
19 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...