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

Remove rows of a certain value, before values change in R

I have a data frame like the following:

dat <- data.frame(Target = c(rep("01", times = 8), rep("02", times = 5), 
                             rep("03", times = 4)),
                         targ2clicks = c(1, 1, 1, 1, 0, 0 ,0 , 1, 1, 0, 0, 0, 1,
                                         0, 0, 0, 1))

    Target targ2clicks
1      01           1
2      01           1
3      01           1
4      01           1
5      01           0
6      01           0
7      01           0
8      01           1
9      02           1
10     02           0
11     02           0
12     02           0
13     02           1
14     03           0
15     03           0
16     03           0
17     03           1

Where the first instance for each Target is 1 in the targ2clicks column, I want to remove all rows from the data frame that have 1 in this column before the first occurrence of 0 for that Target. However, where the first value is 0 for a Target, I want to keep all of the values/rows.

What I want to end up with is:

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

   Target targ2clicks
     01           0
     01           0
     01           0
     01           1
     02           0
     02           0
     02           0
     02           1
     03           0
     03           0
     03           0
     03           1

Where all instances for a Target are 1 with no 0s (not in the example df, but just to consider in any solutions), all rows for that Target should be removed.

I have tried coding this in various different ways with no success! Any help hugely appreciated.

>Solution :

You could use ave() + cumsum():

dat[with(dat, ave(targ2clicks == 0, Target, FUN = cumsum)) > 0, ]

#    Target targ2clicks
# 5      01           0
# 6      01           0
# 7      01           0
# 8      01           1
# 10     02           0
# 11     02           0
# 12     02           0
# 13     02           1
# 14     03           0
# 15     03           0
# 16     03           0
# 17     03           1

Its dplyr equivalent is

library(dplyr)

dat %>%
  group_by(Target) %>%
  filter(cumany(targ2clicks == 0)) %>%
  ungroup()
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