title: In R: Want to find all "subvectors" with a specific length (9) from a large vector (length 300) , such that the elements sum to a given value
body: <p>I'd like to find a way to list all the subvectors from the vector of salaries below, with the requirement that their sum lies between 54,000 and 55,000. I've tried using <a href="https://stackoverflow.com/questions/14948258/given-an-input-array-find-all-subarrays-with-given-sum-k">this</a> method in C, but it takes way too long...I think because it finds the vectors before applying the limits (54,000 &lt; sum(vector) &lt; 55,000). Is there a way to improve on this, and preferably do it in R?</p>
  
  <p>I've asked a friend about this, and he suggests developing a method to apply the sum limits as you go; because otherwise, the program would have to find (300 C 9) ~ 4.8 x 10^16 combinations.</p>
  
  <p>Thanks!</p>
  
  <p>Here's the list of salaries:</p>
  
  <pre><code>Salary
  10800
  9800
  9600
  9600
  9500
  9100
  8800
  8800
  8700
  8600
  8600
  8600
  8600
  8300
  8200
  8200
  8100
  8100
  8100
  8100
  8000
  7900
  7600
  7500
  7500
  7500
  7400
  7300
  7300
  7300
  7200
  7100
  7100
  7100
  7000
  6900
  6900
  6900
  6800
  6800
  6800
  6800
  6700
  6700
  6600
  6600
  6600
  6500
  6500
  6500
  6500
  6500
  6400
  6300
  6300
  6300
  6200
  6200
  6100
  6000
  6000
  6000
  6000
  5900
  5900
  5900
  5800
  5800
  5800
  5800
  5800
  5700
  5700
  5700
  5600
  5600
  5600
  5600
  5600
  5500
  5500
  5500
  5400
  5400
  5400
  5400
  5400
  5300
  5200
  5200
  5200
  5200
  5200
  5200
  5200
  5200
  5200
  5200
  5100
  5100
  5100
  5100
  5000
  5000
  5000
  5000
  5000
  5000
  5000
  5000
  5000
  5000
  5000
  5000
  4900
  4900
  4800
  4800
  4800
  4800
  4800
  4700
  4700
  4700
  4700
  4700
  4700
  4600
  4600
  4600
  4500
  4500
  4500
  4500
  4500
  4400
  4400
  4400
  4400
  4300
  4300
  4300
  4300
  4300
  4300
  4200
  4200
  4200
  4200
  4200
  4200
  4200
  4100
  4100
  4100
  4100
  4100
  4100
  4000
  4000
  4000
  4000
  4000
  4000
  4000
  4000
  3900
  3900
  3900
  3900
  3900
  3900
  3900
  3800
  3800
  3800
  3800
  3800
  3800
  3800
  3800
  3700
  3700
  3700
  3700
  3700
  3700
  3600
  3600
  3600
  3600
  3600
  3600
  3600
  3600
  3600
  3600
  3600
  3600
  3500
  3500
  3500
  3500
  3500
  3500
  3500
  3500
  3500
  3500
  3500
  3500
  3500
  3500
  3400
  3400
  3400
  3400
  3400
  3400
  3400
  3400
  3400
  3400
  3400
  3400
  3400
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3300
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3200
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3100
  3000
  3000
  3000
  3000
  3000
  3000
  3000
  3000
  3000
  3000
  3000
  3000
  3000
  3000
  </code></pre>