How to Delete Rows in R? Explained with Examples (2024)

R provides a subset() function to delete or drop a single row/multiple rows from the DataFrame (data.frame), you can also use the notation [] and -c() to delete the rows. In this article, we will discuss several ways to delete rows from the DataFrame. We can delete rows from the data frame in the following ways:

  1. Delete Single/Multiple Rows from a DataFrame by row index
  2. Delete Multiple Rows from a DataFrame by row name
  3. Delete Rows by Condition

Note that R doesn’t have a function that deletes the Rows from the R DataFrame however, we should use a subsetting way to drop rows. For example, to delete the second and third row in R, You can use -c(1, 3), and it will return the DataFrame without the first and third rows.

1. Quick Examples of Delete Rows

Below are some quick examples of deleting or dropping rows from the R DataFrame.

# Below are the qucik examples# Exampe 1: delete 4th rowdf2 <- df[-4,]# Exampe 2: delete 4th,5th and 1st rowsdf2 <- df[-c(4,5,1),]# Exampe 3: delete rows by rangedf2 <- df[-(1:3),]# Exampe 4: delete rows by namedf2 <- df[!(row.names(df) %in% c("1","2")),]# Exampe 5:Remove rows with column id less than equal 2df2 <- subset(df,id > 2 )

Let’s create a DataFrame with 5 rows and 3 columns.

# Create dataframe with 5 rows and 3 columnsdf = data.frame(id=c(1,2,3,4,5), name=c('sravan','srinu','chrisa','shivgami','jim'), gender=c('m','m','f','f','m'))# Display dataframedf

Yields below output.

How to Delete Rows in R? Explained with Examples (1)

2. Delete Rows by Index from the R DataFrame

To delete single row/multiple rows from R DataFrame(data.frame) you can use [] notation with the negative row index. Here, we delete only a single row from the R DataFrame using the row index number. The row number starts with 1.

Syntax:

# Syntaxdf[-row_index,]

Where df is the DataFrame from where you want to delete the row? Note that this doesn’t remove from the existing DataFrame so you need to assign it to another DataFrame to get the desired result.

Let’s remove the specified row by using its corresponding row_index.

# Remove specified row by index df2 <- df[-4,]df2

Output:

How to Delete Rows in R? Explained with Examples (2)

In the above code, we have deleted the 4th row and returned a new DataFrame with the remaining rows.

3. Delete Multiple Rows from the R DataFrame

You can also delete multiple rows by row index. For that, you can specify indexes of rows that you want to delete from DataFrame using a c() function. This function creates a vector containing the row numbers you want to exclude from the data frame df. The - sign before c(...) is used to remove those specific rows from the data frame.

Syntax:

# Syntaxdf[-c(row_number1,row_number2,.........),]

Example:

In this example, we will delete multiple rows at a time.

#delete 4th,5th and 1st rowsdf2 <- df[-c(4,5,1),]df2

Output:

# Output id name gender2 2 srinu m3 3 chrisa f

In the above code, we have deleted the 4th, 5th, and 1st rows at a time. so the output will be the remaining rows present in the data frame.

4. Delete Rows by Range

You can also delete rows by range using bracket notation. The following example drops all rows from row number 1 and 3.

# delete rows by rangedf2 <- df[-(1:3),]df2

Output:

# Output id name gender4 4 shivgami f5 5 jim m

5. Delete Rows by Row Name

In our DataFrame we don’t have custom row names instead row names are the same as row index numbers due to default behavior. If you have row names then you may need to delete rows by name. The below example demonstrates how you can do this.

# delete rows by namedf2 <- df[!(row.names(df) %in% c("1","2")),]df2

Output:

# Output id name gender3 3 chrisa f4 4 shivgami f5 5 jim m

6. Delete Rows by Condition

In this scenario, we are selecting only a particular row from a data frame based on the condition, this way, we can ignore the rows from the data frame that fails the condition using the subset() method. This method takes two parameters data frame object and the condition.

Syntax:

# Syntaxsubset(df,condition )

In this example, we will apply different conditions.

# Remove rows with column id less than equal 2df2 <- subset(df,id > 2 )df2# Remove rows where gender not equal to 'm'df2 <- subset(df,gender=='m' )df2

Output:

# Output 1 id name gender3 3 chrisa f4 4 shivgami f5 5 jim m# Output 2 id name gender1 1 sravan m2 2 srinu m5 5 jim m

In the above code

  • Output 1 – Selectedrowsbasedonthe idcolumnsuchthatvaluesintherowaregreaterthan2.
  • Output 2 – Selectedrowsbasedongendercolumnsuchthatvaluesintheroware equalto‘m’.

Frequently Asked Questions on Delete Rows in R

How do I delete specific rows from a data frame in R?

To delete specific rows from a data frame, you can use the negative index notation. For example, if you want to delete rows 3 and 5, df <- df[-c(3, 5), ]

How can I remove rows with missing values in R?

To remove rows with missing values, you can use the na.omit() function or the complete.cases() function. For example, df <- df[complete.cases(df), ]

What is the difference between subset() and direct subsetting for deleting rows?

The subset() function allows you to specify conditions for row removal more conveniently. For example, df <- subset(df, condition)

How do I delete duplicate rows from a data frame?

You can use the duplicated() function to identify the duplicate rows and remove them. For instance, df <- df[!duplicated(df), ]

How to delete rows with specific values in a column?

You can use logical subsetting to delete rows with specific values in a column. For example, df <- df[df$column_name != "specific_value", ]

7. Conclusion

In this article, we have seen three methods to delete or drop single and multiple rows from the DataFrame in R language, Based on the requirement of your application, you can use any of the above-discussed methods.

Related Articles

  • Sort R DataFrame Rows by Multiple Columns
  • How to Delete File or Directory in R?
  • R Sort DataFrame Rows by Column Value
  • How to Drop Columns by Name in R?
  • R Remove From Vector with Examples
  • How to Remove Duplicate Rows in R
  • How to Remove Rows with NA in R

References

How to Delete Rows in R? Explained with Examples (2024)

FAQs

How do I delete rows of data in R? ›

How to Remove Rows in R?
  1. Using Subsetting. One of the most straightforward ways to remove rows in R is by subsetting the data. ...
  2. Using the subset() Function. ...
  3. Removing Duplicates. ...
  4. Using the dplyr Package. ...
  5. Removing Rows by Row Index. ...
  6. Deleting Rows by Range. ...
  7. Deleting Rows by Name.
Sep 19, 2023

How do I delete a list of rows in R? ›

Remove Rows from the data frame in R
  1. Remove any rows containing NA's. df %>% na. omit() ...
  2. Remove any rows in which there are no NAs in a given column. df %>% filter(! is. ...
  3. Get rid of duplicates. df %>% distinct() ...
  4. Remove rows based on their index position. df %>% filter(! ...
  5. Based on the condition, remove rows.
Jun 3, 2022

How to remove rows from dataframe in R based on a condition? ›

To remove rows of data from a dataframe based on a single conditional statement we use square brackets [ ] with the dataframe and put the conditional statement inside it. This slices the dataframe and removes all the rows that do not satisfy the given condition.

How do I delete a row with certain values? ›

You can highlight only the range that contains the values you want to remove. Then use the shortcut Ctrl + - (minus on the main keyboard) to get the standard Excel Delete dialog box allowing you to select the Entire row radio button, or any other deleting option you may need.

How do I delete rows in dataset? ›

Delete(); If a row is marked for deletion and you call the AcceptChanges method of the DataTable object, the row is removed from the DataTable. In contrast, if you call RejectChanges, the RowState of the row reverts to what it was before being marked as Deleted.

How do I remove multiple rows from a dataset in R? ›

R provides a subset() function to delete or drop a single row/multiple rows from the DataFrame (data. frame), you can also use the notation [] and -c() to delete the rows.

How do I delete repetitive rows in R? ›

Remove duplicate rows in a data frame

The function distinct() [dplyr package] can be used to keep only unique/distinct rows from a data frame. If there are duplicate rows, only the first row is preserved. It's an efficient version of the R base function unique() .

How to select certain rows in R? ›

To limit your dataset to a subset of observations in base R, use brackets [ ] or subset() . With brackets you can subset based on row numbers, row names, or a logical expression. With subset() , you must use a logical expression. Selecting a subset of observations is called filtering.

How to delete values in R? ›

remove and rm are identical R functions that can be used to remove objects. These can be specified successively as character strings, or in the character vector list , or through a combination of both. All objects thus specified will be removed.

How do you delete specific rows in DataFrame? ›

Another method to remove rows with specific values in a Pandas DataFrame is to use the drop function. This function allows us to remove rows or columns based on their labels or positions. We can use this function to remove all rows that contain the specific value we want to remove.

How do I delete rows with specific values in a DataFrame? ›

To delete rows from a DataFrame based on a specific column value, you can use the drop method in Pandas. The drop method takes an argument index , which is a list of row labels to delete.

How do I subtract rows in a DataFrame in R? ›

To subtract one data frame from another in R, you can use the "-" operator, enabling you to compare or compute differences between datasets.

How to delete rows based on cell value? ›

To remove rows based on cell value, highlight the range in your worksheet where you want to remove rows. Next, open the 'Find and Replace' window by simultaneously holding down 'CTRL' and 'F'. In the 'Find and Replace' window, enter the cell value (for this example, we use 'Cost') into the 'Find' box.

How do I delete all rows with a specific value in a column? ›

Removing all rows containing specific value using Filter

Press Ctrl + Shift + L to enable Filters. Alternatively, you can click Filter in Data tab. Click the down arrow in the header of the column that contains the value you want to select. Select the value(s) you want to remove in the filter dialog.

How do I delete blank rows? ›

Right-click on any selected cell and choose "Delete row" from the context menu or just press Ctrl + - (minus sign). Click OK in the "Delete entire sheet row?" dialog box.

How do I delete data and values in R? ›

We use the rm() function for this purpose. By providing the argument list = ls(), we ensure that all variables are removed from the list of objects in the current session. Keep in mind that this will also remove any functions you may have assigned in the session.

Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 6567

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.