Subset data frame based on multiple conditions
RDataframeSubsetR Problem Overview
I wish to filter a data frame based on conditions in several columns. For example, how can I delete rows if column A = B and Column E = 0.
R Solutions
Solution 1 - R
Logic index:
d<-d[!(d$A=="B" & d$E==0),]
Solution 2 - R
Subset is your safest and easiest answer.
subset(dataframe, A==B & E!=0)
Real data example with mtcars
subset(mtcars, cyl==6 & am!=0)
Solution 3 - R
Use the 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, ]