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,010 points
2,984 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 darklord
• 6,140 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 16, 2018 in Data Analytics by DataKing99
• 8,100 points
45 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,680 points
54 views
+1 vote
1 answer

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

You have missed out on "na.rm=TRUE" inside ...READ MORE

answered Mar 27, 2018 in Data Analytics by Bharani
• 4,550 points
31 views
0 votes
1 answer
0 votes
1 answer

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

I would suggest you, to never to ...READ MORE

answered Apr 26, 2018 in Data Analytics by kappa3010
• 2,010 points
59 views
0 votes
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,700 points
32 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 4, 2018 in Data Analytics by Kalgi
• 35,800 points
23 views
0 votes
1 answer
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
54 views
0 votes
1 answer

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

You have various options to count the ...READ MORE

answered Apr 12, 2018 in Data Analytics by darklord
• 6,140 points
38 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.