Using the logical condition and operator in a data frame, we can remove rows in r based on conditions.

In this tutorial, we will discuss how to remove a row from a data frame in R, remove multiple rows in R and how to remove rows from a data frame in R based on condition.

How to remove a row from a data frame in R

Using the logical condition in the data frame using the square bracket notation, we can remove the row from a data frame in R.

Let’s practice with an example to delete a column from a data frame.

Create a data frame in R using the data.frame function.

# Create a data frame
emp_info <- data.frame(
  id = c(1,2,3,4,5,6),
  name = c("Henry","Gary","Sam","Julie","Kim","Chris"),
  age = c(28,24,29,25,23,24),
  salary = c(4500,4000,5500,3650,3400,3800)
)

# Print data frame
emp_info

In the above R code, it creates a data frame named emp_info and prints the data frame. The Data frame has 4 columns and 6 rows as given below.

  id  name age salary
1  1 Henry  28   4500
2  2  Gary  24   4000
3  3   Sam  29   5500
4  4 Julie  25   3650
5  5   Kim  23   3400
6  6 Chris  24   3800

We can remove one or multiple rows from a data frame in R using the logical condition.

Let’s delete a single row from a data frame based on condition.

# Delete a single column from a data frame in R
emp_info[emp_info$id != 3,]

In the above R code, we have specified a condition that gets the rows from the data frame where id is not equal to 3.

The output of the above R code to drop row from a data frame in R is:

  id  name age salary
1  1 Henry  28   4500
2  2  Gary  24   4000
4  4 Julie  25   3650
5  5   Kim  23   3400
6  6 Chris  24   3800

Row Id 3 is removed from the data frame.

Remove Rows from a Data Frame based on Multiple Conditions

Using the logical condition with operators like &, OR, !=, we can remove rows from a data frame in R based on multiple conditions specified.

Let’s practice deleting multiple rows from the data frame in R.

Use the above sales_data data frame for illustration purposes.

# Remove multiple rows from a data frame based on certain values matches the condition
emp_info[emp_info$id != 3 & emp_info$salary > 3500,]

In the above R code, we have specified multiple conditions like employee id should not equal to 3 and salary should be greater than 3500.

It will remove multiple rows from a data frame based on these conditions where conditions match the criteria. The output of the above R code is:

  id  name age salary
1  1 Henry  28   4500
2  2  Gary  24   4000
4  4 Julie  25   3650
6  6 Chris  24   3800

It gets all the employees whose salary is greater than 3500 and removes employed id 3 and salary below 3500 from a data frame.

Delete Multiple Rows from a Data Frame using subset

Using the subset() function in R, we can remove multiple rows from a data frame based on conditions.

Let’s use the above data frame emp_info to drop multiple rows in R.

# Remove rows from a data frame based on condition
subset(emp_info, emp_info$id != 3 & emp_info$salary > 3500)

In the above R code, the subset function uses data frame as input parameter, and multiple conditions like employee id not equal to 3 and salary should be greater than 3500.

The output of the above R code removes one or more rows from a data frame based on conditions in R.

id  name age salary
1  1 Henry  28   4500
2  2  Gary  24   4000
4  4 Julie  25   3650
6  6 Chris  24   3800

Conclusion

I hope the above article on how to remove rows from a data frame in R based on condition is helpful to you.

Using the logical condition and operator in square bracket notation inside the data frame, we can delete rows in R.

Using the subset() function in R, we can remove rows from a data frame using multiple conditions.