ire, TIBCO Software
wdunlap tibco.com
> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
> Behalf
> Of Jeff Newmiller
> Sent: Tuesday, August 07, 2012 10:25 AM
> To: R. Michael Weylandt; number10
> Cc: r-help@r-project.org
Another approach... not exactly more direct, but perhaps more robust and more
general:
tmp <- aggregate(x,list(lvl=cumsum(abs(diff(c(FALSE,x>0), FUN=sum)
ans <- tmp[0==tmp$lvl,"x"]
abs(diff()) finds transitions, FALSE forces level zero to represent negative
numbers
cumsum marks levels (grou
I'd do something like this:
x <- sample(seq(-10, 10))
sum(x[seq_len(which.max(x > 0)])
Though others might have more direct solutions.
which.max() gets you the index of the first time x > 0 -- seq_len
gives you numbers 1 to that index -- then just subset and sum like
normal.
Best,
Michael
On
Hi, Is it possible to avoid using do and while loops to calculate the sum of
the elements of a vector until the appearance of the first positive element.
--
View this message in context:
http://r.789695.n4.nabble.com/Sum-of-vector-elements-tp4639395.html
Sent from the R help mailing list archiv
4 matches
Mail list logo