How to remove rows with missing values NAs in a data frame

0 votes

I want to remove the rows with missing values(NAs). Below is my example data frame:

             u       v   w     x   y    z
1 ABCD00000207234    0   NA   NA   NA   NA
2 ABCD00000198674    0   2    2    2    2
3 ABCD00000223622    0   NA   NA   NA   NA
4 ABCD00000200604    0   NA   NA   1    2
5 ABCD00000201431    0   NA   NA   NA   NA
6 ABCD00000220312    0   1    2    3    2

Basically, I'd like to get a data frame such as the following.

             u       v    w    x    y   z
2 ABCD00000198674    0    2    2    2    2
6 ABCD00000220312    0    1    2    3    2

Also,If I want to filter only few columns? How will I do that?

            u        v   w    x    y    z
2 ABCD00000199674    0   2    2    2    2
4 ABCD00000200604    0   NA   NA   1    2
6 ABCD00000220312    0   1    2    3    2
Apr 13, 2018 in Data Analytics by kappa3010
• 2,090 points
14,385 views

2 answers to this question.

0 votes

You can use complete.cases in the following manner:

final[complete.cases(final), ]
             u       v    w    x    y    z
2 ABCD00000198674    0    2    2    2    2
6 ABCD00000220312    0    1    2    3    2

na.omit can also be chosen to remove all NA's. Also it is better than complete.cases as complete.cases allows partial selection i.e. it includes certain columns of the dataframe:

final[complete.cases(final[ , 5:6]),]
             u       v    w    x    y    z
2 ABCD00000198674    0    2    2    2    2
4 ABCD00000200604    0   NA   NA    1    2
6 ENSG00000220312    0    1    2    3    2

This is not the solution you want right? So to use is.na you have to use something like this:

final[rowSums(is.na(final[ , 5:6])) == 0, ]
             u       v    w    x    y    z
2 ABCD00000198674    0    2    2    2    2
4 ABCD00000200604    0   NA   NA    1    2
6 ABCD00000220312    0    1    2    3    2
answered Apr 13, 2018 by Sahiti
• 6,370 points
0 votes
Hi,

The below code returns rows without NA values.

dataframe[-which(is.na(dataframe)), ]

To filter out few you can add row numbers next to which() to display the NA as well as other filtered rows.

rows = c(-which(is.na(airquality)),row_numbers.....)

dataframe[-which(is.na(dataframe)), ]

Hope it helps!
answered Aug 20, 2019 by anonymous
• 33,030 points

Related Questions In Data Analytics

0 votes
1 answer

How to filter a data frame with dplyr and tidy evaluation in R?

Requires the use of map_df to run each model, ...READ MORE

answered May 17, 2018 in Data Analytics by DataKing99
• 8,240 points
1,609 views
0 votes
1 answer

How to write a custom function which will replace all the missing values in a vector with the mean of values in R?

Consider this vector: a<-c(1,2,3,NA,4,5,NA,NA) Write the function to impute ...READ MORE

answered Jul 4, 2018 in Data Analytics by CodingByHeart77
• 3,740 points
4,190 views
+1 vote
2 answers

Custom Function to replace missing values in a vector with the mean of values

Try this. lapply(a,function(x){ifelse(is.na(x),mean(a,na.rm = TRUE ...READ MORE

answered Aug 14, 2019 in Data Analytics by anonymous
1,610 views
0 votes
1 answer
0 votes
2 answers

How to subset rows containing NA in a chosen column of a data frame?

You can give this a try. subset(dataframe, is.na(dataframe$col2)) ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
9,750 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
1,229 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 5, 2018 in Data Analytics by Kalgi
• 52,360 points
743 views
0 votes
1 answer
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,401 views
+1 vote
2 answers

How to count the number of elements with the values in a vector?

Use dplyr function group_by(). > n = as.data.frame(num) > ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
4,527 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP