dimanche 16 septembre 2018

R dyplr - remove lines if statements

I have a dataframe which looks like this:

Status  ID 
  A     1
  B     1
  B     1
  A     1
  B     1
  A     1
  A     2
  A     2
  A     2
  A     2
  B     3
  B     3
  B     3

To illustrate my desired output, please have a look at below:

Status  ID 
  B     1
  B     1
  B     1
  A     2
  A     2
  A     2
  A     2
  B     3
  B     3
  B     3

As you can see, the only thing that changes is for group ID = 1. If a group contains both a "A" and "B" status, I'd like to remove the "A" status.

However, Group ID 2 and 3 did not change (ie no lines removed) because: if each group ID only contains a "A", then it will remain the same. Likewise, if each group ID only contains a "B", it will also remain the same. Hence both remains the same.

Using dyplyr, this is my attempt:

df1_clean <- df1 %>% group_by(ID, Status)
                 %>% filter(ifelse((Status == A | Status == B), Status == B,
                     ifelse((Status == A), Status == A,
                     ifelse((Status == B), Status == B))))

However, this filter will not work. Any help would be appreciated!

Aucun commentaire:

Enregistrer un commentaire