Open In App

Remove Rows with Missing Values using R

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
3 Likes
Like
Report

Missing values are the data points that are absent for a specific variable in a dataset. It can be represented in various ways such as Blank spaces, null values or any special symbols like"NA". Because of these various reasons missing values can occur, such as data entry errors, malfunction in equipment...etc. Dealing with missing data is a crucial step in data analysis.

Mainly used methods are:

  • na.omit()
  • complete.cases()

1. Remove rows with missing values using na.omit()

The na.omit() function is used for removing NA values that were present in the dataset row-wise. This function checks each row and removes any row that contains one or more NA values, which works more efficiently in manner while dealing with missing values.

For example, we have a data frame consisting of rows and columns, if we use the na.omit() function then all NA values are removed from the data frame on row-wise basis.

Syntax:

na.omit(dataframe )

Example 1:

Here, we created a sample data frame. After using function na.omit(), all NA values are removed ,which were present in the data frame by row-wise.

R
df1= data.frame(  
  A1 = c(NA, 10, NA, 7, 8, 11,20),
  A2 = c("A", 9, 3, "B", "C", "D","E"),
  A3 = c(1, 0, NA, 1, 1, NA,3)
)

return(df1)
print("After removing the NA values ")
result=na.omit(df1)
return(result)

Output:

na1
Remove rows with missing values using R

Example 2:

Here, created a sample data frame with the help of vectors. After using the function na.omit() ,it removes all NA values which were present in the dataframe by row-wise.

R
vec1=c(1,2,NA,4,8,4)
vec2=c(6,7,8,9,2,9)
vec3=c(34,NA,67,78,23,12)

df1=data.frame(vec1,vec2,vec3)

return(df1)
print("After removing NA values: ")
df2=na.omit(df1)
return(df2)

Output:

na2
Remove rows with missing values using R

2. Remove rows with missing values using complete.cases()

The complete.cases() is used for removing missing data in a data frame , matrix or in a vector. This function can easily filter the rows with missing data and works more efficient in manner . This function is mostly useful, when you want to remove the data based on missing values.

For example, if we have a dataset and we want to remove rows that have missing values, then we can use complete.cases() function.

Syntax:

complete.cases(dataframe)

Example 1:

Here we create a sample data frame and use the complete.cases() function to remove the NA values from the data frame.

R
df1 <- data.frame(  
  A1 = c(NA, 10, NA, 7, 8, 11,20),
  A2 = c("A", 9, 3, "B", "C", "D","E"),
  A3 = c(1, 0, NA, 1, 1, NA,3)
)

return(df1)
print("After removing the NA values ")
result=df1[complete.cases(df1),]
return(result)

Output:

complete1
Remove rows with missing values using R

Example 2:

Here we created a sample data frame with the help of vectors. After using the function complete.cases() , we removed all NA values from the data frame.

R
vec1 = c(1,2,NA,4,8,4)
vec2 = c(6,7,8,9,2,9)
vec3 = c(34,NA,67,78,23,12)

return(df1)

print("After removing the NA values ")
result=df1[complete.cases(df1),]
return(result)

Output:

complete2
Remove rows with missing values using R

In this article, we exploreed various methods to remove rows containing missing values (NA) in the R Programming Language.


Article Tags :

Explore