Subset data frame based on multiple conditions

RDataframeSubset

R Problem Overview


enter image description here

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, ]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAMEView Question on Stackoverflow
Solution 1 - RmbqView Answer on Stackoverflow
Solution 2 - RTyler RinkerView Answer on Stackoverflow
Solution 3 - RManuel RamónView Answer on Stackoverflow