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