[R] Conditionally remove rows with logic

jim holtman jholtman at gmail.com
Wed Aug 10 19:13:53 CEST 2016


try this:

> input <- read.table(text = "ID     TIME     LABEL
+  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)
>
>  result <- do.call(rbind,
+     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
+     })
+ )
>
>
> result
     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
> wrote:

> Dear all,
>
> I need to remove any rows AFTER the label becomes 1.  For example, for ID
> 1, the two rows with TIME of 15 & 18 should be removed; for ID 2, any rows
> after time 6, i.e., rows of time 9-18, should be removed.  Any
> suggestions?  Thank you very much!
>
> The current dataset looks like the following:
> ID     TIME     LABEL
> 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
>
> Thanks a lot!
> Jennifer
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list