-3

So I know how to delete rows based off one column condition. How do I do it for multiple conditions?

Date Frame = "olddata"

Letter Number Color
X 1 Blue
Y 3 Red
Z 5 Yellow
D 1 Red
F 1 Yellow
X 2 Green

For example, to delete rows with the Letter X in it, and rows with color green, you can do:

newdata <- subset(olddata, olddata$Letter != "X" & olddata$Color != "Green")

Now how do I delete Rows that have the letter F and is Yellow, i.e. both criteria must be met.

Thanks!

user6472523
  • 211
  • 3
  • 8
  • 3
    Combine your criteria and then negate it - `olddata[!(olddata$Letter == "F" & olddata$Color == "Yellow"),]` – thelatemail Feb 26 '18 at 21:48
  • already saw that question and the formula didn't work for me: – user6472523 Feb 26 '18 at 21:53
  • tried d<-d[!(d$A=="B" & d$E==0),] and it didn't remove any rows for whatever reason. stared at it for like an hour and didn't see anything wrong with syntax. subset worked but it didn't allow me to give 2 criteria, only 1 – user6472523 Feb 26 '18 at 21:54
  • Are you sure, because the answer there is identical in logic to my comment above, and I can confirm it works with your current data. – thelatemail Feb 26 '18 at 21:55
  • 1
    `subset` will allow multiple criteria for sure - `subset(mtcars, cyl == 4 & mpg > 30)` for a reproducible example. – thelatemail Feb 26 '18 at 21:59
  • 1
    Look carefully. The difference between the answer below and the one given by @thelatemail is in parentheses. thelatemail correctly negates the entire condition, while the answer below does it to the first term only. – Artem Sokolov Feb 26 '18 at 22:03
  • in this: subset(mtcars, cyl == 4 & mpg > 30) I'm pretty sure that removes any row that has either cyl = 4 and or mpg >30. It does not remove just the rows that are BOTH cyl = 4 and mpg >30. At least that what happens when I used that formula – user6472523 Feb 28 '18 at 15:11
  • @user6472523 - *"I'm pretty sure that removes any row that has either cyl = 4 and or mpg >30"* - actually it *keeps* any row that has `cyl==4` **and** `mpg>30`. You'd use `subset(mtcars, !(mtcars$cyl == 4 & mtcars$mpg > 30))` if you wanted to remove the rows. – thelatemail Mar 04 '18 at 23:58

2 Answers2

0
newdata <- olddata[!(olddata$Letter == "F" & olddata$Color == "Yellow"),]
thelatemail
  • 91,185
  • 12
  • 128
  • 188
AidanGawronski
  • 2,055
  • 1
  • 14
  • 24
  • That would remove both rows 3 and 6. It's just like my old formula. I want to remove just row 6. – user6472523 Feb 26 '18 at 21:44
  • If you're going to use `subset`, no need for `olddata$` again each time. – thelatemail Feb 26 '18 at 21:47
  • @thelatemail, that's good to know. I prefer base R and I assumed subset basically functioned the same way, changing my answer to base R. – AidanGawronski Feb 26 '18 at 21:54
  • No problems. `subset` is base R by the way, just a 'non-standard evaluation' function that R provides. Also, I think you need to negate your whole selection - at the moment you're keeping "F"/"Yellow" rows rather than deleting them. – thelatemail Feb 26 '18 at 21:57
  • lol... yes ... yes I am. – AidanGawronski Feb 26 '18 at 21:59
  • Just to continue to be a nuisance - `!olddata$Letter == "F" & olddata$Color == "Yellow"` is not the same as `!(olddata$Letter == "F" & olddata$Color == "Yellow")` - check each result in the console (I didn't downvote btw). – thelatemail Feb 26 '18 at 22:02
  • I feel like I'm in a slap stick routine. lol. – AidanGawronski Feb 26 '18 at 22:06
-2

Use:

library(dplyr)

olddata %>%
filter(olddata, Letter!="F" & Color!="Yellow")
Daniel V
  • 1,305
  • 7
  • 23
Hello.World
  • 720
  • 8
  • 22