Open In App

Delete rows with empty cells from Excel using R

Last Updated : 05 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Practically during any survey for data collection, getting all the information from all units is not possible as 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 help to deal with missing information.

Also, reading the excel file with some empty cells may also produce errors sometimes, which ultimately affect the model accuracy. Therefore, the removal of empty cells is an important process.

To remove rows with empty cells we have a syntax in the R language, with which it is very easier for the user to remove as many numbers of empty rows in the excel file.

Steps –

  • Import data
  • Select rows with empty cells
  • Delete such rows
  • Copy to original data frame

Syntax:

data <- data[!apply(data == “”, 1, all),]

Example 1:

File used:

R




gfg_data=read.csv('input_gfg.csv')
  
gfg_data <- gfg_data[!apply(is.na(gfg_data) | gfg_data == "", 1, all),]
  
print(gfg_data)


Output:

Example 2:

File in use:

R




library(readxl)
  
gfg_data <- read_excel("Data_gfg.xlsx")
print("original dataset:-")
print(gfg_data)
  
gfg_data <- gfg_data[!apply(is.na(gfg_data) | gfg_data == "", 1, all),]
print("Modified dataset:-")
print(gfg_data)


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads