R provides a subset() function to delete or drop a single row and multiple rows from the DataFrame (data.frame), you can also use the notation [] and -c(). In this article, we will discuss several ways to delete rows from the data frame. We can delete rows from the data frame in the following ways:
- Delete Rows by Row Number from a data frame
- Delete Rows by Row Number
- Delete Multiple Rows from a data frame
- Delete Rows by Condition
Note that R doesn’t have a function that deletes the Rows from the R data frame however, we should use a subsetting way to drop rows. For example, to delete the second and third row in R, use -c(1, 3)
, and it will return the data frame without the first and third row.
1. Quick Examples of Delete Rows
Below are some quick examples of how to delete or drop rows from the R DataFrame.
#delete 4th row
df2 <- df[-4,]
#delete 4th,5th and 1st rows
df2 <- df[-c(4,5,1),]
# delete rows by range
df2 <- df[-(1:3),]
# delete rows by name
df2 <- df[!(row.names(df) %in% c("1","2")),]
#Remove rows with column id less than equal 2
df2 <- subset(df,id > 2 )
Let’s create a data frame with 5 rows and 3 columns.
#create dataframe with 5 rows and 3 columns
df = data.frame(id=c(1,2,3,4,5),
name=c('sravan','srinu','chrisa','shivgami','jim'),
gender=c('m','m','f','f','m'))
#display dataframe
df
Yields below output.
# Output
id name gender
1 1 sravan m
2 2 srinu m
3 3 chrisa f
4 4 shivgami f
5 5 jim m
2. Delete Rows by Row Number from R Dataframe
In order to delete rows by row number from an R data frame (data.frame) using []
notation with the negative row index. Here, we are deleting only a single row from the R data frame using the row number. Row number starts with 1.
Syntax:
# Syntax
df[-row_index,]
Where df
is the data frame from where you wanted to delete the row. Note that this doesn’t remove from the existing DataFrame so you need to assign it to another data frame to get the desired result.
#remove 4th row
df2 <- df[-4,]
df2
#remove 5th row
df2 <-df[-5,]
df2
#remove 1st row
df2 <-df[-1,]
df2
Output:
# Output
# Remove 4th Row
id name gender
1 1 sravan m
2 2 srinu m
3 3 chrisa f
5 5 jim m
# Remove 5th Row
id name gender
1 1 sravan m
2 2 srinu m
3 3 chrisa f
4 4 shivgami f
# Remove 1st Row
id name gender
2 2 srinu m
3 3 chrisa f
4 4 shivgami f
5 5 jim m
In the above code, we have deleted 4th, 5th, and 1st rows separately.
3. Delete Multiple Rows from R Dataframe
Use -c()
with the row id you wanted to delete, Using this we can delete multiple rows at a time from the R data frame. Here row index numbers are specified inside vector c().
Syntax:
# Syntax
df[-c(row_number1,row_number2,.........),
Example:
In this example, we will delete multiple rows at a time.
#delete 4th,5th and 1st rows
df2 <- df[-c(4,5,1),]
df2
Output:
# Output
id name gender
2 2 srinu m
3 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 range
df2 <- df[-(1:3),]
df2
Output:
# Output
id name gender
4 4 shivgami f
5 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 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 name
df2 <- df[!(row.names(df) %in% c("1","2")),]
df2
Output:
# Output
id name gender
3 3 chrisa f
4 4 shivgami f
5 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, by 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:
# Syntax
subset(df,condition )
In this example, we will apply different conditions.
#Remove rows with column id less than equal 2
df2 <- subset(df,id > 2 )
df2
#Remove rows where gender not equal to 'm'
df2 <- subset(df,gender=='m' )
df2
Output:
# Output 1
id name gender
3 3 chrisa f
4 4 shivgami f
5 5 jim m
# Output 2
id name gender
1 1 sravan m
2 2 srinu m
5 5 jim m
In the above code
- Output 1 – Selected rows based on the id column such that values in the row are greater than 2.
- Output 2 – Selected rows based on gender column such that values in the row equal to ‘m’.
7. Conclusion
In this article, we have seen three methods to delete or drop single and multiple rows from the data frame in R language, Based on the requirement in your application, you can use any one 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