jeudi 11 février 2021

selecting specific rows based on NA pattern

I want to remove specific observations that occur before two conditions are met. I want to remove observations where B = 1 and A = 1 IF it occurs before A = 1 AND B = 1. I have data that look like this.

PID    Time     A        B
1        2      NA       1     
1        3      1        NA
1        4      1        1
2        1      NA       1       
2        2      1        1 

I want this, where cases that have A = 1 or B = 1 are removed IF they occur before cases where A = 1 AND B = 1.

PID    Time     A        B
1        4      1        1
2        2      1        1 

I tried using ifelse to do this and couldn't get it to work. Can anyone advise to make this work?

Aucun commentaire:

Enregistrer un commentaire