Conditionally delete Dataframe rows using R

Possible duplicate:
Removing specific rows from a data frame

enter image description here

Using R, how can I write the following logic into a data frame: IF column A = B and column E = 0, delete the row

+98
r
Nov 04 '11 at 5:14
source share
3 answers

Logical index:

d<-d[!(d$A=="B" & d$E==0),] 
+245
Nov 04 2018-11-11T00:
source share

A subset is your safest and easiest answer.

 subset(dataframe, A==B & E!=0) 

Real-world example using mtcars

 subset(mtcars, cyl==6 & am!=0) 
+75
Nov 04 2018-11-11T00:
source share

Use which function:

 A <- c('a','a','b','b','b') B <- c(1,0,1,1,0) d <- data.frame(A, B) r <- with(d, which(B==0, arr.ind=TRUE)) newd <- d[-r, ] 
+3
Nov 04 2018-11-11T00:
source share



All Articles