[R] local sequence function

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Sep 14 19:06:09 CEST 2009


try the following:

v <- c(NA,NA,TRUE,TRUE,NA,TRUE,NA,TRUE,TRUE,TRUE)

l <- as.numeric(!is.na(v))
tmp <- cumsum(l)
tmp - cummax((!l) * tmp)


I hope it helps.

Best,
Dimitris


smu wrote:
> hey,
> 
> I can not find a function for the following problem, hopefully you can
> help me.
> 
> I have a vactor like this one
> 
> v = c(NA,NA,TRUE,TRUE,NA,TRUE,NA,TRUE,TRUE,TRUE)
> 
> and I would like to the TRUE values by the their "local sequence
> number".
> 
> This means, the result should look thike this:
> 
> c(NA,NA,1,2,NA,1,NA,1,2,3)
> 
> Of course I could solve the problems using a loop, but this would be
> much to slow, because the real vector is much larger.
> Can you point me in the right direction?
> 
> thank you!
> 
> regards,
>  Stefan
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list