How to Remove Rows in R DataFrame?
Last Updated :
19 Dec, 2021
In this article, we will discuss how to remove rows from dataframe in the R programming language.
Method 1: Remove Rows by Number
By using a particular row index number we can remove the rows.
Syntax:
data[-c(row_number), ]
where.
- data is the input dataframe
- row_number is the row index position
Example:
R
# create a dataframe
data=data.frame(name=c("manoj","manoja","manoji","mano","manooj"),
age=c(21,23,21,10,22))
# display by removing 4 th row
print(data[-c(4), ])
# display by removing 5 th row
print(data[-c(5), ])
# display by removing 1 st row
print(data[-c(1), ])
Output:
> print(data[-c(4), ])
name age
1 manoj 21
2 manoja 23
3 manoji 21
5 manooj 22
> print(data[-c(5), ])
name age
1 manoj 21
2 manoja 23
3 manoji 21
4 mano 10
> print(data[-c(1), ])
name age
2 manoja 23
3 manoji 21
4 mano 10
5 manooj 22
We can also remove multiple rows by using the slice operator
Syntax:
data[-c(row_number_start:row_number_end), ]
where,
- data is the input dataframe
- row_number_start is the starting row number
- row_number_end is the ending row number
Example:
R
# create a dataframe
data=data.frame(name=c("manoj","manoja","manoji","mano","manooj"),
age=c(21,23,21,10,22))
# display by removing row from 1 st to 4 th
print(data[-c(1:4), ])
# display by removing row from 1 st to 2 nd
print(data[-c(1:2), ])
Output:
name age
5 manooj 22
>
> # display by removing row from 1 st to 2 nd
> print(data[-c(1:2), ])
name age
3 manoji 21
4 mano 10
5 manooj 22
We can also remove multiple rows by specifying multiple row indices
Syntax:
data[-c(row numbers), ]
Example:
R
# create a dataframe
data = data.frame(name=c("manoj", "manoja", "manoji", "mano",
"manooj"), age=c(21, 23, 21, 10, 22))
# display by removing 1,3,4 rows
print(data[-c(1, 3, 4), ])
Output:
name age
2 manoja 23
5 manooj 22
Method 2: Remove rows conditionally
We can also use the conditions using subset() function
Syntax:
subset(dataframe,condition )
Example:
R
# create a dataframe
data=data.frame(name=c("manoj","manoja","manoji","mano","manooj"),
age=c(21,23,21,10,22))
# display by removing age less than 21
print(subset(data,age>21 ))
Output:
name age
2 manoja 23
5 manooj 22
Method 3: Remove rows with NA values:
we can remove rows that contain NA values using na.omit() function from the given data frame.
Syntax:
na.omit(dataframe)
Example:
R
# create a dataframe
data=data.frame(name=c("manoj","manoja","manoji","mano","manooj"),
age=c(21,23,21,10,22))
# display by removing age less than 21
print(na.omit(data))
Output:
name age
1 manoj 21
2 manoja 23
3 manoji 21
4 mano 10
5 manooj 22
Similar Reads
How to Remove Duplicate Rows in R DataFrame? In this article, we will discuss how to remove duplicate rows in dataframe in R programming language. Dataset in use:Method 1: Using distinct() This method is available in dplyr package which is used to get the unique rows from the dataframe. We can remove rows from the entire which are duplicates a
2 min read
How to Conditionally Remove Rows in R DataFrame? In this article, we will discuss how to conditionally remove rows from a dataframe in the R Programming Language. We need to remove some rows of data from the dataframe conditionally to prepare the data. For that, we use logical conditions on the basis of which data that doesn't follow the condition
4 min read
How to remove empty rows from R dataframe? A dataframe can contain empty rows and here with empty rows we don't mean NA, NaN or 0, it literally means empty with absolutely no data. Such rows are obviously wasting space and making data frame unnecessarily large. This article will discuss how can this be done. To remove rows with empty cells w
1 min read
How to Retrieve Row Numbers in R DataFrame? In this article, we will discuss how to Retrieve Row Numbers in R Programming Language. The dataframe column can be referenced using the $ symbol, which finds its usage as data-frame$col-name. The which() method is then used to retrieve the row number corresponding to the true condition of the speci
2 min read
Remove First Row of DataFrame in R In this article, we are going to see how to remove the first row from the dataframe. We can remove first row by indexing the dataframe. Syntax: data[-1,] where -1 is used to remove the first row which is in row position Example 1: R program to create a dataframe with 2 columns and delete the first
1 min read
How to Delete Row(s) in R DataFrame ? In this article, we will see how row(s) can be deleted from a Dataframe in R Programming Language. Deleting a single row For this, the index of the row to be deleted is passed with a minus sign. Syntax: df[-(index), ] Example 1 :Â R # creating a data frame with # some data . df=data.frame(id=c(1,2,3
2 min read