Reputation: 215
I have tried to look through these examples https://www.datasciencemadesimple.com/delete-or-drop-rows-in-r-with-conditions-2/ Delete rows based on multiple conditions in r but its now working on my code
I seem to be able to delete all of station 7, or not delete any, but I only want to delete depth 1 and depth 2 of station 7 but keep depth 3. Is this possible?
Station <- c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7,7,8, 8, 8, 9, 9,9)
Depth <- c(1, 2, 3, 1, 2, 3,1, 2, 3,1, 2, 3,1, 2, 3,1, 2, 3,1, 2, 3,1, 2, 3,1, 2, 3)
Value <- c(5, 8, 3, 2, 6, 8, 3, 6, 3, 8, 3, 5, 7, 2, 6, 9, 1, 3, 456, 321, 2, 5, 7, 4, 2, 6, 8)
df <- data.frame(Station, Depth, Value)
df
a <- df[!(df$Station == 7 & df$Depth == 1 ) | !(df$Station == 7 & df$Depth == 2 ),]
a
Upvotes: 0
Views: 2629
Reputation: 388862
Here are couple of ways to write this -
subset(df, !(Station == 7 & Depth %in% 1:2))
Or -
subset(df, Station != 7 | Station == 7 & Depth == 3)
The same expression can also be used in dplyr::filter
if you prefer that.
Upvotes: 1
Reputation: 2419
Try
a <- df[!( (df$Station == 7 & df$Depth == 1 ) | (df$Station == 7 & df$Depth == 2 )),]
a
or more compact one
a <- df[!( df$Station == 7 & (df$Depth == 1 | df$Depth == 2 )),]
a
Upvotes: 2