Home > Enterprise >  delete rows based on value in a column
delete rows based on value in a column

Time:02-07

I have a df like this;

NEW_df <- data.frame(CAR = c("YES","NO","MAYBE","YES","NO"),
                     BIKE = c(2,0,4,NA,9),
                     PLANE = c(8,0,6,7,9),
                     BOAT = c(0,0,0,4,0),
                     SCOOTER = c(2,3,6,9,0))

I want to delete only rows where column BOAT is '0', when column CAR is "YES" or "MAYBE" so I have something like this;

NEW_df2 <- data.frame(CAR = c("NO","YES","NO"),
                     BIKE = c(0,NA,9),
                     PLANE = c(0,7,9),
                     BOAT = c(0,4,0),
                     SCOOTER = c(3,9,0))

I have tried this:

NEW_df[NEW_df$BOAT %in% subset(NEW_df, CAR == "YES")$BOAT, ] 

NEW_df[NEW_df$BOAT %in% subset(NEW_df, CAR == "MAYBE")$BOAT, ] 

I'm sure I am doing something wrong.

CodePudding user response:

Here's a tidyverse dplyr solution using filter(). Same idea as the base R answer. Create a condition that matches BOAT == 0 & CAR %in% c("YES", "MAYBE") and filter to keep the opposite using the not ! operator:

library(dplyr)

NEW_df2 <- NEW_df %>% 
  filter(!(BOAT == 0 & CAR %in% c("YES", "MAYBE" )))

print(NEW_df2)
> print(NEW_df2)
  CAR BIKE PLANE BOAT SCOOTER
1  NO    0     0    0       3
2 YES   NA     7    4       9
3  NO    9     9    0       0

CodePudding user response:

I think this does it? Specify the rows that match those conditions, then subset the rows for the opposite.

NEW_df[-which(new_DF$CAR %in% c("YES", "MAYBE") & new_DF$BOAT == 0),]

  CAR BIKE PLANE BOAT SCOOTER
2  NO    0     0    0       3
4 YES   NA     7    4       9
5  NO    9     9    0       0

CodePudding user response:

For subset, you don't need to wrap it in brackets. You can just include both conditions like so:

subset(NEW_df, !(BOAT ==0 & CAR %in% c("YES", "MAYBE")))

  CAR BIKE PLANE BOAT SCOOTER
2  NO    0     0    0       3
4 YES   NA     7    4       9
5  NO    9     9    0       0

data.table option:

library(data.table)

dt <- NEW_df
setDT(dt)[!(CAR %in% c("YES", "MAYBE") & BOAT == 0)]

   CAR BIKE PLANE BOAT SCOOTER
1:  NO    0     0    0       3
2:  NO    9     9    0       0
3: YES   NA     7    4       9
  •  Tags:  
  • Related