Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Delete data frame lines with multiple variables values conditions R

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

> df
[1] m Y N
<0 rows> (or 0-length row.names)

Any requests ?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading