Complete Cases in R with Examples

[This article was first published on Data Analysis in R » Quick Guide for Statistics & R » finnstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post Complete Cases in R with Examples appeared first on finnstats.

If you want to read the original article, click here Complete Cases in R with Examples.

Are you looking for the latest Data Science Job Vacancies / Internship then click here finnstats.

We encourage that you read this article from finnstats to stay up to date..

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!

To read more visit Complete Cases in R with Examples.

If you are interested to learn more about data science, you can find more articles here finnstats.

The post Complete Cases in R with Examples appeared first on finnstats.

To leave a comment for the author, please follow the link and comment on their blog: Data Analysis in R » Quick Guide for Statistics & R » finnstats.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)