To understand a detailed analysis of the dataset, sometimes, we want to remove specific rows and clear the data to analyze more efficiently. This is where subsetting is essential and, in other words deleting specific rows.
How to remove rows in R
To remove the rows in R, use the subsetting in R. There is no built-in function of removing a row from the data frame, but you can access a data frame without some rows specified by the negative index. This process is also called subsetting. This way, you can remove unwanted rows from the data frame.
Syntax of subsetting
df[-c(row_index_1, row_index_2),]
Parameters
The df is the data frame.
The row_index_1, row_index_2, . . . are the comma-separated indices that should be removed in the resulting data frame.
It takes a vector of row indexes that needs to be removed. You can pass a single value to remove the single row or multiple elements vector to remove multiple rows.
Example
Let’s define a data frame.
df <- data.frame(
Shares = c("TCS", "Reliance", "HDFC Bank", "HUL"),
Price = c(3200, 1900, 1500, 2200)
)
df
Output
Shares Price
1 TCS 3200
2 Reliance 1900
3 HDFC Bank 1500
4 HUL 2200
Let’s remove the third row from the data frame. To remove the third row, you need to follow the below snippet.
dfRemain <- df[-c(3), ]
We passed a negative index 3 means third row. It will subset the data frame and returns the data frame without a third row. See the entire code below.
df <- data.frame(
Shares = c("TCS", "Reliance", "HDFC Bank", "HUL"),
Price = c(3200, 1900, 1500, 2200)
)
dfRemain <- df[-c(3), ]
dfRemain
Output
Shares Price
1 TCS 3200
2 Reliance 1900
4 HUL 2200
You can see that we got the data frame without a third row.
Deleting multiple rows in R
To remove the multiple rows in R, use the subsetting and pass the vector with multiple elements. The elements are the row index, which we need to remove.
To remove the second and third-row in R, use -c(2, 3), and it will return the data frame without the second and third row.
df <- data.frame(
Shares = c("TCS", "Reliance", "HDFC Bank", "HUL"),
Price = c(3200, 1900, 1500, 2200)
)
dfRemain <- df[-c(2, 3),]
dfRemain
Output
Shares Price
1 TCS 3200
4 HUL 2200
You can see that two rows have been removed from the data frame, and the indices of the removed rows are (2, 3).
df <- data.frame(
Shares = c("TCS", "Reliance", "HDFC Bank", "HUL"),
Price = c(3200, 1900, 1500, 2200)
)
dfRemain <- df[c(TRUE, FALSE, FALSE, TRUE), ]
dfRemain
Output
Shares Price
1 TCS 3200
4 HUL 2200
That is it for this tutorial.

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.