Hi, I need help to find an efficient way to transform a vector like: a<-c(1,1,0,1,0,0,0,1,1,1,1,0,1,0,1,1) in a vector that counts only di 1 elements, like: b<-c(1,2,0,1,0,0,0,1,2,3,4,0,1,0,1,2) Thank you! -- View this message in context: http://r.789695.n4.nabble.com/counts-of-a-vector-tp2232047p2232047.html Sent from the R help mailing list archive at Nabble.com.
Check out this thread for numerous solutions: https://stat.ethz.ch/pipermail/r-help/2007-June/134557.html On Wed, May 26, 2010 at 1:22 PM, speretti <sabrina.peretti at gmail.com> wrote:> > Hi, > > I need help to find an efficient way to transform a vector like: > > a<-c(1,1,0,1,0,0,0,1,1,1,1,0,1,0,1,1) > > in a vector that counts only di 1 elements, like: > > b<-c(1,2,0,1,0,0,0,1,2,3,4,0,1,0,1,2) > > > Thank you! > -- > View this message in context: http://r.789695.n4.nabble.com/counts-of-a-vector-tp2232047p2232047.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
speretti wrote:> Hi, > > I need help to find an efficient way to transform a vector like: > > a<-c(1,1,0,1,0,0,0,1,1,1,1,0,1,0,1,1) > > in a vector that counts only di 1 elements, like: > > b<-c(1,2,0,1,0,0,0,1,2,3,4,0,1,0,1,2) > > > Thank you!One way: rl <- rle(a) unlist(mapply("*", lapply(rl$lengths, function(x) 1:x), as.list(rl$values)))
Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of speretti > Sent: Wednesday, May 26, 2010 10:22 AM > To: r-help at r-project.org > Subject: [R] counts of a vector > > > Hi, > > I need help to find an efficient way to transform a vector like: > > a<-c(1,1,0,1,0,0,0,1,1,1,1,0,1,0,1,1) > > in a vector that counts only di 1 elements, like: > > b<-c(1,2,0,1,0,0,0,1,2,3,4,0,1,0,1,2)The following is pretty quick: > f <- function (x) { # x must be entirely composed of 0's and 1's or FALSE's and TRUE's. csx <- cumsum(x) csx - cummax(csx * (1-x)) # 1-x could also be !x } > all.equal(f(a), b) [1] TRUE Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> > > Thank you! > -- > View this message in context: > http://r.789695.n4.nabble.com/counts-of-a-vector-tp2232047p2232047.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
### Special cumsum ### Do cumsum when TRUE, and reset to 0 when FALSE x <- c( TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE ) ## The rle function computes the lengths of equal values of a vector ## And gives two vecters: $lengths and $values ## The sequence function creates and concatenates sequences by a integers vector x*sequence(rle(x)$lengths) ----- A R learner. -- View this message in context: http://r.789695.n4.nabble.com/counts-of-a-vector-tp2232047p2232338.html Sent from the R help mailing list archive at Nabble.com.