Open In App

Drop row(s) by number from given DataFrame in R

In this article, we will be discussing the approaches to drop rows by a number from a given Data Frame in R language. Dropping of rows from a data frame is simply used to remove the unwanted rows in the data frame.

Method 1: Using minus(-) sign

In this method, the user needs to provide the index of the particular row that is needed to be dropped from the data frame.



Syntax:

df<- df[-c(…), ]



The ‘-‘ sign indicates dropping variables.

Approach

Example:




gfg <- data.frame(A=c(5,5,5,5,5), 
                  B=c(4,4,4,4,4), 
                  C=c(3,3,3,3,3))
  
print('Original dataframe:-')
gfg
  
gfg <- gfg[-c(1,3), ]
print('Modified dataframe:-')
gfg

Output:

Method 2: Using index method

In this method user just need to specify the needed rows and the rest of the rows will automatically be dropped.This method can be used to drop rows/columns from the given data frame.

Approach

Syntax:

Dataframe[ rownumber, colnumber]

Example:




gfg <- data.frame(A=c(5,5,5,5,5),
                  B=c(4,4,4,4,4),
                  C=c(3,3,3,3,3))
  
print('Original dataframe:-')
gfg
  
print('Modified dataframe:-')
gfg[2:4, ]

Output:-


Article Tags :