Advertisement
Advertisement


Conditionally Remove Dataframe Rows with R


Question

enter image description here

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

2019/06/26
1
132
6/26/2019 2:16:41 PM

Accepted Answer

Logic index:

d<-d[!(d$A=="B" & d$E==0),]
2011/11/04
311
11/4/2011 9:04:01 AM


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, ]
2011/11/04

Source: https://stackoverflow.com/questions/8005154
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]