I am trying to do a nested ifelse
statement in R where, if a row meets two conditions, then delete, if not, keep. However, I am trying to compare across two dataframes, so I couldn't find other previous questions that helped.
I am trying to essentially say this:
ifelse(D1$A == D2$A,
ifelse(D1$B == D2$B,
"Delete Row",
"Keep Row"),
"Keep Row")
I have a data frame that looks like this:
D1
A B C
123 10 Blue
123 12 Blue
100 7 Blue
and
D2
A B C
123 10 Red
123 12 Red
115 7 Red
To clarify, I want to delete the "Blue" rows that have the same A and B in D2. D2 rows have been classified as Red by other functions, so the rows in D1 that have the same A and B as D2 but are blue are wrong. So when I bind them together, I have rows that are "123,10,Red" and "123,10,Blue" when they should be red.
I want to keep the unique rows in both dataframes, but get rid of the "123,10" in D1 IF it is in D2. The problem is I filtered things out of D1 and put them into D2, and I am trying to rbind them, but there are duplicates. I would delete duplicates when they are bound, but it is not working for some reason.
I tried this:
D2 <- D2[!(D2$A %in% D1$A & D2$B %in% D1$B),]
but I am not getting the right amount of observations. It is deleting 20 more than it should. Thank you!