rdataframefilterdataset

How to keep the first appearance of a value while filtering everything else out in R?


This is the appearance of my dataset currently. I want to include patient 1 data until the first '1' occurs in 'test.result' then remove any information about patient 1 after that.

current dataset

This is what I have tried so far- it removed all the 0 values in my data but didn't delete all the other 1 values for each patient after the first initial 1. I want to keep the 0 values for a patient if they are not preceeded by a 1.

new.ddset <- ddset %>% 
  group_by(id) %>% 
  filter(test.result == max(test.result))

This is what I want in my results:
EX: For id1, at screen.num 1-5, the test.result was 0, but at screen 6, screen.num was 1 - I want to keep all the information for that id up to screen 6 and delete all other id information for id1 after that.


Solution

  • Try the following.

    filter(df, cumsum(test.result)<=1  
           & test.result==cummax(test.result), .by=ID)
    

      ID seq test.result
    1  1   1           1
    2  2   1           0
    3  2   2           1
    4  3   1           0
    5  3   2           0
    6  3   3           0
    7  3   4           1
    

    Data

    df
       ID seq test.result
    1   1   1           1
    2   1   2           0
    3   1   3           1
    4   1   4           0
    5   1   5           0
    6   2   1           0
    7   2   2           1
    8   2   3           0
    9   2   4           0
    10  3   1           0
    11  3   2           0
    12  3   3           0
    13  3   4           1
    
    df <- structure(list(ID = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3), 
        seq = c(1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 3, 4), test.result = c(1, 
        0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1)), class = "data.frame", row.names = c(NA, 
    -13L))