Help with excluding item based on condition on other columns

Hello,

I am trying to exclude some IDs that are "CLOSED" in Type A regardless of the fact if it is NEW, OLD or CLOSED IN TypeB

If I filter using

filter(!(TypeA == "CLOSED" & TypeB == "CLOSED")

Then, it deletes row with CLOSED, but still leaves rows with NEW or OLD
I would like to completely exclude those IDs that are CLOSED in TypeA, regardless if its Closed in TypeB or not.
At the same time, I do want to retain IDs with CLOSED only in Type B and not CLOSED in TypeA.

In below sample, I would like to retain 101, but I would like to exclude 104 & 106

#Sample Code
library(tidyverse)

data <- data.frame(
  ID = c("101","101", "101","104", "105", "104", "106"),
  
  TypeA = c("OLD","NEW","NEW","CLOSED", "NEW", "NEW", "CLOSED"),
  
  TypeB = c("OLD","NEW","CLOSED","CLOSED", "NEW", "NEW", "NEW")
)

Thanks for your help!

Does this do what you want?

data <- data.frame(
  ID = c("101","101", "101","104", "105", "104", "106"),
  
  TypeA = c("OLD","NEW","NEW","CLOSED", "NEW", "NEW", "CLOSED"),
  
  TypeB = c("OLD","NEW","CLOSED","CLOSED", "NEW", "NEW", "NEW")
)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data
#>    ID  TypeA  TypeB
#> 1 101    OLD    OLD
#> 2 101    NEW    NEW
#> 3 101    NEW CLOSED
#> 4 104 CLOSED CLOSED
#> 5 105    NEW    NEW
#> 6 104    NEW    NEW
#> 7 106 CLOSED    NEW
ClosedA <- data |> filter(TypeA == "CLOSED") |> select(ID)
ClosedA
#>    ID
#> 1 104
#> 2 106
dataNew <- anti_join(data, ClosedA, by = "ID")
dataNew
#>    ID TypeA  TypeB
#> 1 101   OLD    OLD
#> 2 101   NEW    NEW
#> 3 101   NEW CLOSED
#> 4 105   NEW    NEW

Created on 2023-06-02 with reprex v2.0.2

1 Like

Yes @FJCC . Thanks you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.