简体   繁体   中英

Summing Vector elements until a Positive element is encountered - R

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)]);

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM