Open In App

How To Remove Row In R

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In R Programming Language you can remove rows from a data frame using various methods depending on your specific requirements. Here are a few common approaches:

Remove Row Using Logical Indexing

You can remove rows based on a logical condition using indexing. For example, to remove rows where a certain column meets a specific condition:

R
# Example data frame
df <- data.frame(
  ID = 1:5,
  Name = c("John", "Alice", "Bob", "Emma", "Michael"),
  Age = c(25, 30, 22, 35, 40)
)
df
# Remove rows where Age is greater than 30
df <- df[df$Age <= 30, ]
# Print the modified data frame
print(df)

Output:

  ID    Name Age
1  1    John  25
2  2   Alice  30
3  3     Bob  22
4  4    Emma  35
5  5 Michael  40

Remove rows where Age is greater than 30
  ID  Name Age
1  1  John  25
2  2 Alice  30
3  3   Bob  22

In this example, the rows where the age is greater than 30 are removed.

Remove Row Using the subset() Function

The subset() function can be used to subset rows based on conditions:

R
# Example data frame
df <- data.frame(
  ID = 1:5,
  Name = c("John", "Alice", "Bob", "Emma", "Michael"),
  Age = c(25, 30, 22, 35, 40)
)
df
# Remove rows where Age is greater than 30
df <- subset(df, Age <= 30)
# Print the modified data frame
print(df)

Output:

  ID    Name Age
1  1    John  25
2  2   Alice  30
3  3     Bob  22
4  4    Emma  35
5  5 Michael  40

Remove rows where Age is greater than 30
  ID  Name Age
1  1  John  25
2  2 Alice  30
3  3   Bob  22

Remove Row Using the filter() Function from dplyr Package

The filter() function from the dplyr package provides a concise way to filter rows based on conditions:

R
library(dplyr)
# Example data frame
df <- data.frame(
  ID = 1:5,
  Name = c("John", "Alice", "Bob", "Emma", "Michael"),
  Age = c(25, 30, 22, 35, 40)
)
df
# Remove rows where Age is greater than 30
df <- filter(df, Age <= 30)
# Print the modified data frame
print(df)

Output:

  ID    Name Age
1  1    John  25
2  2   Alice  30
3  3     Bob  22
4  4    Emma  35
5  5 Michael  40

Remove rows where Age is greater than 30
  ID  Name Age
1  1  John  25
2  2 Alice  30
3  3   Bob  22

Remove Row Using the na.omit() Function

If your data frame contains missing values (NA), you can remove rows with missing values using the na.omit() function.

R
# Example data frame with missing values
df <- data.frame(
  ID = 1:5,
  Name = c("John", "Alice", NA, "Emma", "Michael"),
  Age = c(25, 30, 22, NA, 40)
)
df
# Remove rows with missing values
df <- na.omit(df)
# Print the modified data frame
print(df)

Output:

  ID    Name Age
1  1    John  25
2  2   Alice  30
3  3    <NA>  22
4  4    Emma  NA
5  5 Michael  40

Remove rows with missing values
  ID    Name Age
1  1    John  25
2  2   Alice  30
5  5 Michael  40

These are some common methods for removing rows from a data frame in R. Choose the method that best suits your specific data and requirements.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads