[R] Conditionally remove rows with logic
Jim Lemon
drjimlemon at gmail.com
Mon Aug 8 09:11:52 CEST 2016
Hi Jennifer,
A very pedestrian method, but I think it does what you want.
remove_rows_after_1<-function(x) {
nrows<-dim(x)[1]
rtr<-NA
rtrcount<-1
got1<-FALSE
thisID<-x$ID[1]
for(i in 1:nrows) {
if(x$ID[i] == thisID && got1) {
rtr[rtrcount]<-i
rtrcount<-rtrcount+1
}
if(x$ID[i] != thisID) {
thisID<-x$ID[i]
got1<-FALSE
}
if(x$ID[i] == thisID && x$LABEL[i]) got1<-TRUE
}
return(rtr)
}
The function returns the indices of rows to be removed.
Jim
On Mon, Aug 8, 2016 at 8:21 AM, 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.
More information about the R-help
mailing list