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 6931727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:36:54+00:00 2026-05-27T11:36:54+00:00

I have the following problem and don’t know how to code it in R:

  • 0

I have the following problem and don’t know how to code it in R:

A dataframe df with two columns: First column an ordered set of numbers df$ind, the second column a highly stochastic set of numbers df$ret.

I want to find two ranges in df$ret that don’t overlap and optimize the condition that the sum of the numbers in the first range must be positive and the sum of the numbers of second range negative. After that I would like to have the 2×2 df$ind-values of the respective ranges.

Two possibilites come to my mind (and I don’t know how to program either of these in R):

  1. Monte Carlo where 2×2 positions are being chosen, the sums being calculated and compared to the hitherto best solution.
  2. Trying all possible ranges and taking the best solution (concerning the number of values this seems computationally doable).

Could you give me some hints how to realize this in R or is there even a package for these kinds of optimizations (there seems to be a package for everything in R 😉

UPDATE:
You will have 4 values: i, k, m, n: df$ret[i:k] and df$ret[m:n] with i < k < m < n.

The optimization is (pseudocode):

max:abs(sum(range(i:k)))+abs(sum(range(m:n)))

under the conditions:

sum(range(i:k)) > 0 and sum(range(m:n)) < 0

  • 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-27T11:36:54+00:00Added an answer on May 27, 2026 at 11:36 am

    Here’s a brute force method. For small data sets it should work fine; on my system I tested with size 100 and it was on the order of 0.5 seconds. For increased speed one should check the best max and min for overlap before checking all possible max/min pairs.

    getbest <- function(x) {
      # get the sums of all possible ranges
      n <- length(x)
      m <- as.data.frame(t(combn(n, 2)))
      names(m) <- c("lo","hi")
      m$sum <- sapply(1:nrow(m), function(i) {
        sum(x[m$lo[i]:m$hi[i]])
      })
      # then get the ranges of positive and negative sums that don't overlap
      neg <- m[m$sum<0,]
      pos <- m[m$sum>0,]
      use <- expand.grid(neg=1:nrow(neg), pos=1:nrow(pos))
      use <- use[(neg$hi[use$neg] < pos$lo[use$pos]) | 
                      (neg$lo[use$neg] > pos$hi[use$pos]),]
      # finally get the absolute value for all ranges that don't overlap,
      # and choose the largest
      abs <- pos$sum[use$pos] - neg$sum[use$neg]
      use <- use[which.max(abs),]
      as.matrix(rbind(positive=pos[use$pos,], negative=neg[use$neg,]))
    }
    

    Use as follows; it returns the actual index of the range, so if the desired index df$ind is different than 1:n, just use this output to get the desired values.

    x <- rnorm(100)
    getbest(x)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Q: I have the following problem : i don't know what the problem i
Q: I have the following problem , and i don't know how to fix
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
I'm facing the following problem and don't have an answer to it: We have
I have the following problem: In mysql I have a table which contains two
I have the following problem. Let's take the input (wikitext) ======hello((my first program)) world======
I have following problem: I have built a tabbar application with 4 tabs. I
I have the following problem: I have an HTML textbox ( <input type=text> )
I have the following problem using subversion: I'm currently working on the trunk of
I have the following problem using template instantiation [*]. file foo.h class Foo {

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.