In this tutorial, we will learn how to delete a row or multiple rows from a dataframe in R programming with examples.

You cannot actually delete a row, but you can access a dataframe without some rows specified by negative index. This process is also called subsetting in R language.

To delete a row, provide the row number as index to the Dataframe. The syntax is shown below:

where

  • mydataframe is the dataframe
  • row_index_1, row_index_2, . . . are the comma separated indices which should be removed in the resulting dataframe

A Big Note: You should provide a comma after the negative index vector -c(). If you miss that comma, you will end up deleting columns of the dataframe instead of rows.

Example: Delete Row from Dataframe

Let us create a dataframe, DF1

Let us assume that we need DF1 with 2nd row deleted. The index of 2nd row is ofcourse 2. Now, we will access this dataframe with a negative index and store the result in another Dataframe DF2.

Viola. We have created a new dataframe with a row deleted from the previous dataframe.

Example: Delete Multiple Rows from Dataframe

Let us create a dataframe, DF1

Let us assume that we need DF1 with 2nd and 4th rows deleted. The indices are (2,4). Now, we will access this dataframe with a vector of negative indices and store the result in another Dataframe DF2.

Viola. We have created a new dataframe with multiple rows deleted from the previous dataframe.