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
>>>>> "s" == smu <ml at z107.de> >>>>> on Mon, 14 Sep 2009 17:20:27 +0200 writes:s> hey, I can not find a function for the following problem, s> hopefully you can help me. s> I have a vactor like this one s> v = c(NA,NA,TRUE,TRUE,NA,TRUE,NA,TRUE,TRUE,TRUE) s> and I would like to the TRUE values by the their "local s> sequence number". s> This means, the result should look thike this: s> c(NA,NA,1,2,NA,1,NA,1,2,3) s> Of course I could solve the problems using a loop, but s> this would be much to slow, because the real vector is s> much larger. Can you point me in the right direction? direction? Ok, something along> lapply(lapply(split(v, cumsum(is.na(v))), `[`, -1L), seq_along)$`1` integer(0) $`2` [1] 1 2 $`3` [1] 1 $`4` [1] 1 2 3 -- Martin Maechler, ETH Zurich
Try this:> v <- c(NA,NA,TRUE,TRUE,NA,TRUE,NA,TRUE,TRUE,TRUE) > ick <-rle(v) > foo <- unlist(apply(matrix(ick$lengths),1,seq)) > foo[is.na(v)] <- NA > foo[1] NA NA 1 2 NA 1 NA 1 2 3 -Don At 5:20 PM +0200 9/14/09, 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.-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA 925-423-1062
Try this also: with(rle(v), unlist(sapply(lengths, FUN = seq)) * v) On Mon, Sep 14, 2009 at 12:20 PM, smu <ml@z107.de> 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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
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