0

I'm new to R and I'm looking through a book called "Discovering Statistics using R". Although the book implies you don't need any statistical background, some of the content isn't covered/explained...

I'm trying to sum the elements of a vector starting from position 1 until a positive element is present.

I found this question which is very similar to what I'm trying to achieve. However when I implement it, it doesn't always seem to work (and it sometimes appears to include the first positive element)...

My program is:

  vecA <- runif(10, -10, 10);
  sumA <-sum(vecA [1:min(which(vecA < 0))]);

Is there a more robust way to calculate this without using loops that works every time and doesn't add the positive element? I'm not at the looping stage of my books yet.

I also found this site which asks a similar question but their answer errors:

  sum(vecA [seq_len(which.max(vecA > 0)]);
Community
  • 1
  • 1
Reanimation
  • 3,151
  • 10
  • 50
  • 88

3 Answers3

3

You can use the following code:

sum(vecA * !cumsum(vecA > 0))

This also works if the first element is positive or all elements are negative.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
1

You want to use > not < to sum all elements until the first positive one is reached.

You're currently summing from 1 until the first negative value is reached (including the first negative value).

sum(vecA[1:min(which(vecA>0))-1])

the which() function will return all of the positions of the positive elements, then taking the sum from 1 to the position of the first positive - 1 will guarantee you are summing all of the negative elements

Steve Reno
  • 1,304
  • 3
  • 14
  • 21
1

match function is usually the fastest to find the first occurrence of some element in a vector, so another version of this could look like follows:

first.positive <- match(TRUE, vecA > 0)
sumA <- sum( vecA[ 1 : first.positive ] ) - vecA[first.positive]

This will give you zero if positive element is the first.

df239
  • 471
  • 2
  • 4