- 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 empty rows from an R data frame?
During the survey or any other medium of data collection, getting all the information from all units is not possible. Sometimes we get partial information and sometimes nothing. Therefore, it is possible that some rows in our data are completely blank and some might have partial data. The blank rows can be removed and the other empty values can be filled with methods that helps to deal with missing information.
Example
Consider the below data frame, it has some missing rows and some missing values −
> x1<-c(rep(c(1,2,3),times=5),"","","",2,1) > x2<-rep(c(2,4,"",4,""),each=4) > x3<-rep(c(5,4,2,""),times=c(2,5,3,10)) > df<-data.frame(x1,x2,x3) > df x1 x2 x3 1 1 2 5 2 2 2 5 3 3 2 4 4 1 2 4 5 2 4 4 6 3 4 4 7 1 4 4 8 2 4 2 9 3 2 10 1 2 11 2 12 3 13 1 4 14 2 4 15 3 4 16 4 17 18 19 2 20 1
Here, we can see that the rows 17 and 18 are complete blank that means we do not have any data in them. Hence, we can remove them from the data frame as shown below −
> df[!apply(df == "", 1, all),] x1 x2 x3 1 1 2 5 2 2 2 5 3 3 2 4 4 1 2 4 5 2 4 4 6 3 4 4 7 1 4 4 8 2 4 2 9 3 2 10 1 2 11 2 12 3 13 1 4 14 2 4 15 3 4 16 4 19 2 20 1
- Related Articles
- How to remove last few rows from an R data frame?
- How to remove multiple rows from an R data frame using dplyr package?
- How to remove rows from data frame in R that contains NaN?
- How to remove rows from an R data frame that contains at least one NaN?
- How to remove rows in an R data frame using row names?
- How to remove column names from an R data frame?
- How to remove a column from an R data frame?
- Remove rows from a data frame that exists in another data frame in R.
- How to remove rows that contains all zeros in an R data frame?
- How to create an empty data frame in R?
- How to remove rows from an R data frame based on frequency of values in grouping column?
- How to randomly sample rows from an R data frame using sample_n?
- How to delete rows in an R data frame?
- How to add rows in an R data frame?
- How to remove underscore from column names of an R data frame?

Advertisements