Re: [R] local sequence function
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Henrique > Dallazuanna > Sent: Monday, September 14, 2009 9:59 AM > To: smu > Cc: r-help@r-project.org > Subject: Re: [R] local sequence function > > Try this also: > > with(rle(v), unlist(sapply(lengths, FUN = seq)) * v) Note that the sequence() function is essentially the second argument to that call to with(). > sequence function (nvec) unlist(lapply(nvec, seq_len)) It uses lapply because there is no need to waste the time sapply spends "simplifying" the answer and it uses seq_len since that is faster than seq (and gives more predictable results for odd inputs like 0). For long input vectors the following Sequence() function is faster and probably uses less memory Sequence <- function(nvec) { seq_len(sum(nvec)) - rep(cumsum(c(0L,nvec[-length(nvec)])), nvec) } Hence the following can be considerably faster than the above f2 <- function(v)v * Sequence(rle(is.na(v))$lengths) Note that I use rle(is.na(v)) instead of rle(v), since R's rle considers a run of n NA's to be n runs of singleton NA's, which would cause the lapply-based function to waste time computing seq_len(1) for each NA (which we intend to throw away). When using Sequence instead of sequence you may as well omit the is.na. For a vector of 10 million NA's and TRUE's with c. 5 million runs in it v1<-sample(c(TRUE,NA), replace=T, size=1e7, prob=c(.7,.3)) f2 takes 2.7 seconds on my machine and the sequence() based approach takes 19.0. sequence() is a candidate for implementing in C as it is orthogonal to the rep() function which is a .Primitive > noquote(rep(c("A","B","C","D"), c(2,4,1,2))) [1] A A B B B B C D D > sequence(c(2,4,1,2)) [1] 1 2 1 2 3 4 1 1 2 Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com > > On Mon, Sep 14, 2009 at 12:20 PM, 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@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]] > > __ 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.
Re: [R] local sequence function
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@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 __ 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.
Re: [R] local sequence function
Try this also: with(rle(v), unlist(sapply(lengths, FUN = seq)) * v) On Mon, Sep 14, 2009 at 12:20 PM, 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@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]] __ 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.
Re: [R] local sequence function
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@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 __ 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.
Re: [R] local sequence function
> "s" == smu > 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 __ 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.