Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:25:15+00:00 2026-05-26T13:25:15+00:00

I have a vector that looks like this: mass<-c(-2, -6, -79, 31, -28, 198,

  • 0

I have a vector that looks like this:

mass<-c(-2, -6, -79, 31, -28, 198, 132, 0, 262, -187, -475, 701, 926)

I need to sum the following subset of values from this vector:

  • all positive values;
  • negative values if they are preceded in the vector by positive value.

So in the example vector above, I would like to exclude -2, -6, and -79 from the sum (they do not follow positive values), but include -28, -187, and -475 (since they are preceded in the vector by positive values).

I can sum. positive values with

sum(mass[mass>0])

But I’m not sure how to include only those negative values that meet my criteria.

(I have a large number of vectors I need to perform a similar operation for, and they do not all have the same sequence of negative/positive values, so I also can’t just deal with this problem by subsetting the vector to exclude the first three values, since the number of values to be excluded will differ depending on the vector).

Thank you very much for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:25:15+00:00Added an answer on May 26, 2026 at 1:25 pm
    ones.you.want <- intersect(which(mass < 0), which(mass > 0)[1]:length(mass))
    sum(mass[ones.you.want])
    

    Note: This could fail if there are no positive values in the vector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector in R that looks like this: dat <- c(TRUE, TRUE,
I have a vector, v3, that looks like this: v3 <- matrix(c(1,1,3,3,2,2, 2,3,1,2,3,1, 3,2,2,1,1,3),
I have a vector that looks like this: dataset <- c(4,7,9,1,10,15,18,19,3,16,10,16,12,22,2,23,16,17) I would like
Have a vector of vectors that looks something like this 3 1 2 0
Say I have a source of CPU side data that looks like this: vector<
I have a C++ objects that looks like this class myClass { vector<OtherClass*> otherClassVector;
Basically I have a function that roughly looks like this and I need to
I have a class that looks like this : #include <iostream> #include <vector> using
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];
I have a large nested vector that look like this: import Data.Vector let x

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.