Complete Cases in R with Examples

Complete cases in R, To eliminate missing values from a vector, matrix, or data frame, use the complete.cases() function in R.

The following is the fundamental syntax for this function.

How to Read rda file in R (with Example) » finnstats

You can delete any values that are missing from the vector

vector <- x[complete.cases(x)]

In any column in the data frame, remove rows with missing values.

df <- df[complete.cases(df), ]

Now, in certain columns of the data frame, eliminate entries with NA.

Correlation Analysis Different Types of Plots in R » finnstats

df <- df[complete.cases(df[ , c('col1', 'col2', ...)]), ]

Complete Cases in R with Examples

The examples below demonstrate how to utilize this function in practice.

Approach 1: Remove any values that are missing from the vector.

To delete all NA values from a vector, use the following code,

vect <- c(5, 3, 4,5, NA, 64, 25, NA, 19)

We can now delete the NA values from the vector.

Principal Component Analysis in R » finnstats

vect <- vect [complete.cases(vect)]
vect
[1]  5  3  4  5 64 25 19

Approach 2: Rows having NA in any column of the data frame should be removed.

The following code explains how to remove rows from a data frame that have NA values in any column,

Let’s create a data frame,

df <- data.frame(A=c(10, 2, NA, 16, NA, 23),
                 B=c(NA, 45, 45, 12, NA, 18),
                 C=c(NA, 45, 12, 5, 18, 22))
df
   A  B  C
1 10 NA NA
2  2 45 45
3 NA 45 12
4 16 12  5
5 NA NA 18
6 23 18 22

In any column data frame, eliminate rows with a NA value.

eXtreme Gradient Boosting in R » Ultimate Guide » finnstats

df <- df[complete.cases(df), ]
df

Approach 3: Rows containing NA in specific columns of a data frame should be removed.

The following code explains how to remove rows from a data frame that have NA values in certain columns,

df <- data.frame(A=c(10, 2, NA, 16, NA, 23),
                 B=c(NA, 45, 45, 12, NA, 18),
                 C=c(NA, 45, 12, 5, 18, 22))
df
   x  y  z
1  1 NA NA
2 24  3  7
3 NA  4  5
4  6  8 15
5 NA NA  7
6  9 12 14

Rows with a NA value in the A or B column should be removed.

Time Series Trend Analysis in R » finnstats

df <- df[complete.cases(df[ , c('A', 'B')]), ]df
   A  B  C
2  2 45 45
4 16 12  5
6 23 18 22

Subscribe to our newsletter!

[newsletter_form type=”minimal”]

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

two × four =