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.
使用 which 功能:
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, ]
子集是最安全最简单的答案。
subset(dataframe, A==B & E!=0)
真实数据例子与 mtcar
subset(mtcars, cyl==6 & am!=0)
逻辑索引:
d<-d[!(d$A=="B" & d$E==0),]