[R] Sum of vector elements

William Dunlap wdunlap at tibco.com
Tue Aug 7 20:27:28 CEST 2012


I'll add my suggestion, which first extracts from x the elements
up to but not including the first positive one and then sums them:
   f3 <- function(x) { sum(x[cumsum(x>0) == 0]) }
Michaels suggestion was
   sum(x[seq_len(which.max(x > 0)])
but that includes the first positive element so I think it should be
at least
   f1 <- function(x) sum(x[seq_len(which.max(x>0)-1)])
(but that still fails in some cases).
Jeff's was
  f2 <- function (x) {
      tmp <- aggregate(x, list(lvl = cumsum(abs(diff(c(FALSE, x > 0))))), FUN = sum)
      tmp[0 == tmp$lvl, "x"]
  }   

To test them, make a list of datasets
> xs <- list(allNeg=c(-1, -3, -9), allPos=c(1,3,9), firstPos=c(1,-3,-9), firstNeg=c(-1,-3,+9), misc=c(-1,-3,9,-27,-81))
> sapply(xs, function(x)try(f1(x)))
  allNeg   allPos firstPos firstNeg     misc 
       0        0        0       -4       -4 
> sapply(xs, function(x)try(f2(x)))
$allNeg
[1] -13

$allPos
numeric(0)

$firstPos
numeric(0)

$firstNeg
[1] -4

$misc
[1] -4

> sapply(xs, function(x)try(f3(x)))
  allNeg   allPos firstPos firstNeg     misc 
     -13        0        0       -4       -4


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 Jeff Newmiller
> Sent: Tuesday, August 07, 2012 10:25 AM
> To: R. Michael Weylandt; number10
> Cc: r-help at r-project.org
> Subject: Re: [R] Sum of vector elements
> 
> 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 (groups of positive and not-positive numbers)
> aggregate does the summation
> ans may be empty if x started with positive numbers.
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                       Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
> 
> "R. Michael Weylandt" <michael.weylandt at gmail.com> wrote:
> 
> >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 Tue, Aug 7, 2012 at 7:37 AM, number10 <havana_dream at ymail.com>
> >wrote:
> >> 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 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.
> >
> >______________________________________________
> >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.
> 
> ______________________________________________
> 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.



More information about the R-help mailing list