I wanted to delete lines from a data frame based on multiple variables values.
In my example below, I wanted to delete lines from March 2022 (and only them). This means that both of my conditions must be true (m = March AND Y = 2022) to have a kind of overall condition True. I tried using subset
or filter
function as I think dplyr
package is a way easier and powerfull. However i did not succeed to delete these lines. Here's my code with subset
function as example :
library(tidyverse)
m <- c("March","March","March", "April", "May","June")
Y <- c("2022","2022","2021","2022","2022","2022")
N <- c(152,75,869,421,748,52)
df <- data.frame(m,Y,N)
df
df<- df %>%
subset(m != "March" & Y !="2022")
df
Here's the console return :
> df
[1] m Y N
<0 rows> (or 0-length row.names)
Any requests ?
CodePudding user response:
Rephrase the filter
to simultaneously include both criteria:
df %>%
filter(!(m == "March" & Y == "2022"))
m Y N
1 April 2022 421
2 May 2022 748
3 June 2022 52