![]() |
VOOZH | about |
In this article, we will discuss how to remove rows from dataframe in the R programming language.
By using a particular row index number we can remove the rows.
Syntax:
data[-c(row_number), ]
where.
Example:
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,
Example:
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:
Output:
name age 2 manoja 23 5 manooj 22
We can also use the conditions using subset() function
Syntax:
subset(dataframe,condition )
Example:
Output:
name age 2 manoja 23 5 manooj 22
we can remove rows that contain NA values using na.omit() function from the given data frame.
Syntax:
na.omit(dataframe)
Example:
Output:
name age 1 manoj 21 2 manoja 23 3 manoji 21 4 mano 10 5 manooj 22