- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove NA’s from an R data frame that contains them at different places?
If NA values are placed at different positions in an R data frame then they cannot be easily removed in base R, we would be needing a package for that. The best package to solve this problem is dplyr and we can use summarise_each function of dplyr with na.omit to remove all the NA’s. But if we have more than one column in the data frame then the number of non-NA values must be same in all the columns.
Example
Consider the below data frame:
> x1<-rep(c(NA,2,3),times=c(7,10,3)) > x2<-rep(c(15,NA,24,NA,18),times=c(5,2,5,5,3)) > df1<-data.frame(x1,x2) > df1
Output
x1 x2 1 NA 15 2 NA 15 3 NA 15 4 NA 15 5 NA 15 6 NA NA 7 NA NA 8 2 24 9 2 24 10 2 24 11 2 24 12 2 24 13 2 NA 14 2 NA 15 2 NA 16 2 NA 17 2 NA 18 3 18 19 3 18 20 3 18
Loading dplyr package and removing NA’s from df1:
Example
> library(dplyr) > df1%>%summarise_each(funs(na.omit(.)))
Output
x1 x2 1 2 15 2 2 15 3 2 15 4 2 15 5 2 15 6 2 24 7 2 24 8 2 24 9 2 24 10 2 24 11 3 18 12 3 18 13 3 18
Let’s have a look at another example:
Example
> y1<-rep(c(545,NA,524,NA,589,NA,537,NA,541,NA),times=c(2,2,2,2,2,2,2,2,2,2)) > y2<-rep(c(NA,2.1,NA,1.7,NA),times=c(4,4,4,6,2)) > df2<-data.frame(y1,y2) > df2
Output
y1 y2 1 545 NA 2 545 NA 3 NA NA 4 NA NA 5 524 2.1 6 524 2.1 7 NA 2.1 8 NA 2.1 9 589 NA 10 589 NA 11 NA NA 12 NA NA 13 537 1.7 14 537 1.7 15 NA 1.7 16 NA 1.7 17 541 1.7 18 541 1.7 19 NA NA 20 NA NA
Removing NA’s from df2:
> df2%>%summarise_each(funs(na.omit(.)))
Output
y1 y2 1 545 2.1 2 545 2.1 3 524 2.1 4 524 2.1 5 589 1.7 6 589 1.7 7 537 1.7 8 537 1.7 9 541 1.7 10 541 1.7
- Related Articles
- How to remove NA’s from an R data frame that contains them at different places?
- How to remove rows from an R data frame that contains at least one NaN?
- How to remove rows from data frame in R that contains NaN?
- How to remove rows that contains all zeros in an R data frame?
- How to remove a column from a data frame that contains same value in R?
- How to remove rows in R data frame that contains a specific number?
- How to remove rows that contains NA values in certain columns of an R data frame?
- How to remove empty rows from an R data frame?
- How to remove column names from an R data frame?
- How to remove a column from an R data frame?
- How to remove row that contains maximum for each column in R data frame?
- How to remove rows that contains coded missing value for all columns in an R data frame?
- How to remove last few rows from an R data frame?
- How to subset an R data frame by specifying columns that contains NA?
- How to remove underscore from column names of an R data frame?
- How to replace NA’s to a value of selected columns in an R data frame?

Advertisements