title: How to prevent myvector[3:(L-2)] reading backwards when L<5?
body: <p>I am processing records from a large dataset with varying lengths using <code>data.table[, somefunc(someseries), by=]</code>. The length L of each record <code>someseries</code> could be anything from 1 to 50. I want to handle the following efficiently without needlessly adding an <code>if</code> expression:</p>
  
  <p>For each group, I want the simplest way to access its middle entries <code>someseries[3:(L-2)]</code> </p>
  
  <p><strong>Problem:</strong> beware that when L&lt;5, the expression <code>someseries[3:(L-2)]</code> actually misbehaves by inferring backwards direction. This is due to the default "helpful" behavior of <code>[from:to]</code> which uses 
  <code>seq(from..., to..., by = ((to - from)/(length.out - 1) ...)</code> i.e. infers backwards direction <code>by=-1</code></p>
  
  <p>In that case I just want somefunc to get passed an empty <code>vector()</code> not <code>someseries[4:2]</code></p>
  
  <p>But you can't explicitly do <code>seq(... by=1)</code> because that errors if <code>from &gt; to</code>.</p>
  
  <p>Here's a testcase:</p>
  
  <pre><code>set.seed(15)
  ragged_arrays &lt;- lapply(ceiling(runif(5,1,5)), function(n) (1:n) )
  # indexing with unwanted auto-backwards
  lapply(ragged_arrays, function(someseries) someseries[2 : (length(someseries)-2)] )
  </code></pre>
  
  <p>For the sake of our testcase, <code>somefunc</code> is a function which behaves gracefully when passed an empty vector, e.g. <code>median()</code></p>