Delete or Drop rows in R with conditions

Drop rows in R with conditions can be done with the help of subset () function. Let’s see how to delete or drop rows in R with an example.

Let’s first create the dataframe.

# create dataframe
df1 = data.frame(Name = c('George','Andrea', 'Micheal','Maggie','Ravi','Xien','Jalpa'), 
                 Grade_score=c(4,6,2,9,5,7,8),
                 Mathematics1_score=c(45,78,44,89,66,49,72),
                 Science_score=c(56,52,45,88,33,90,47))
df1

So the resultant dataframe will be

Delete or Drop rows in R with conditions R 1

 

Delete or Drop rows in R with conditions:

Method 1:

Delete rows with name as George or Andrea

df2<-df1[!(df1$Name=="George" | df1$Name=="Andrea"),]
df2

Resultant dataframe will be

Delete or Drop rows in R with conditions R 2

 

Method 2:

Drop rows with conditions in R using subset function.

df2<-subset(df1, Name!="George" & Name!="Andrea")
df2

Resultant dataframe will be

Delete or Drop rows in R with conditions R 3
Delete or Drop rows in R with conditions - image previous-small-11-1 on http://www.datasciencemadesimple.com                                                                                                              Delete or Drop rows in R with conditions - image next_small-11-1 on http://www.datasciencemadesimple.com