Skip to content
Prev 363263 / 398502 Next

Conditionally remove rows with logic

try this:
+  1        0            0
+  1        3            0
+  1        6            0
+  1        9            0
+  1        12          1
+  1        15          0
+  1        18           0
+  2        0            0
+  2        3            0
+  2        6            1
+  2        9            0
+  2        12          0
+  2        15          0
+  2        18          0", header = TRUE)
+     lapply(split(input, input$ID), function(.id){
+         indx <- which(.id$LABEL == 1)
+         if (length(indx) == 1) .id <- .id[1:indx, ]  # keep upto the '1'
+         .id
+     })
+ )
ID TIME LABEL
1.1   1    0     0
1.2   1    3     0
1.3   1    6     0
1.4   1    9     0
1.5   1   12     1
2.8   2    0     0
2.9   2    3     0
2.10  2    6     1
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Sun, Aug 7, 2016 at 6:21 PM, Jennifer Sheng <jennifer.sheng2002 at gmail.com