title: How to fix "Error in x[(i - z):(i + y)]: only 0's may be mixed with negative subscripts?
body: <p>I have this dataset which includes all the sales for a company in a given year (company code = <code>gvkey</code>, year = <code>fyeqarq</code>, sales = <code>realsales</code>). After calculating the yearly growth rates for sales (<code>growth_rates_adjusted</code>), I try to calculate the volatility of these growth rates. The function used to work with a previous dataset, but is now displaying the above-mentioned error message:</p>
  <blockquote>
  <p>Error in x[(i - 4):(i + 5)] : only 0's may be mixed with negative subscripts &quot;.</p>
  </blockquote>
  <p>The only difference from the current dataset to the previous one, is that it has more observations.</p>
  <p>I've seen other threads where it suggested to add brackets in <code>x[i - 4:i + 5]</code>, but that didn't solve the issue.</p>
  <p>Here is the code segment where the issue is happening. I'll be happy to provide further info if required.</p>
  <pre><code>volatility &lt;- function(x){
    out &lt;- rep(NA, length(x))
    for(i in (1+4):(length(x)-5)){
      out[i] &lt;- sqrt((x[i] - mean(x[(i-4):(i+5)]))**2 /10)
    }
    return(out)
  }
  
  rs$volatility &lt;- do.call(&quot;c&quot;, by(rs$growth_rate_adjusted, rs$gvkey, volatility))
  </code></pre>
  <p>Sample from data:</p>
  <pre><code>structure(list(gvkey = c(1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 
  1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 
  1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 
  1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 
  1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1004L, 1009L, 
  1009L, 1009L, 1009L, 1009L, 1009L, 1009L, 1009L, 1009L, 1009L, 
  1009L, 1009L, 1009L, 1010L, 1010L, 1010L, 1010L, 1010L, 1010L, 
  1010L, 1010L, 1010L, 1010L, 1010L, 1010L, 1010L, 1010L, 1010L, 
  1010L, 1010L, 1010L, 1010L, 1010L, 1010L, 1010L, 1010L, 1010L, 
  1010L, 1010L, 1011L, 1011L, 1011L, 1011L, 1011L, 1011L, 1011L, 
  1011L, 1011L, 1011L, 1011L, 1011L, 1011L, 1011L, 1013L, 1013L, 
  1013L, 1013L, 1013L, 1013L), fyearq = c(1978L, 1979L, 1980L, 
  1981L, 1982L, 1983L, 1984L, 1985L, 1986L, 1987L, 1988L, 1989L, 
  1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 
  1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 
  2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2016L, 
  2017L, 2018L, 1983L, 1984L, 1985L, 1986L, 1987L, 1988L, 1989L, 
  1990L, 1991L, 1992L, 1993L, 1994L, 1995L, 1978L, 1979L, 1980L, 
  1981L, 1982L, 1983L, 1985L, 1986L, 1987L, 1988L, 1989L, 1990L, 
  1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 
  2000L, 2001L, 2002L, 2003L, 2004L, 1982L, 1983L, 1984L, 1985L, 
  1986L, 1987L, 1988L, 1989L, 1990L, 1991L, 1992L, 1993L, 1994L, 
  1995L, 1982L, 1983L, 1984L, 1985L, 1986L, 1987L), realsales = c(80.0787687494763, 
  83.9880322921772, 72.041255647813, 90.2599788213429, 82.3597655295938, 
  79.275697273582, 99.6691746379825, 98.6531307837496, 119.231183761451, 
  130.671738596922, 152.401835452683, 177.004920650438, 182.331045530932, 
  163.067223699202, 145.669513553658, 142.637840975044, 138.071087623594, 
  1686.92046529749, 1944.33679952995, 2503.3850493653, 3430.96847893947, 
  3399.78918423465, 2846.26399241784, 2106.69883444041, 1878.94931648818, 
  1904.88753225045, 2111.0686482661, 2440.41820615184, 2821.72739990673, 
  3568.88502043553, 3764.78124834279, 3431.75866279315, 4563.51718325686, 
  5125.57023176176, 5364.4, 4324.89803940838, 3825.52083333333, 
  3945.16600024812, 3652.23958087506, 3927.3539111424, 1291.78670435397, 
  6.98252898777813, 8.38595031948278, 9.05059463817779, 9.0048500089815, 
  7.90239455877713, 7.75665012953149, 13.1293381562225, 12.8158816415637, 
  12.7200911507786, 13.9056813962124, 246.549622751016, 308.437038299807, 
  214.374965214003, 500.237423535655, 649.926491449795, 635.909445745511, 
  500.535949689884, 483.239029553041, 205.834769664682, 105.739312088838, 
  82.683671636429, 91.7347403849525, 98.6318765979783, 139.826310815655, 
  131.670619277222, 133.607292062286, 101.488303007798, 126.121590249565, 
  138.43476531424, 1371.26398397061, 1626.47062842463, 1721.53939149708, 
  1698.52423521911, 1545.76404347257, 1387.97100335562, 1199.52374984334, 
  976.286828209051, 5855.71181123345, 2177.63623496108, 1.60587804282341, 
  1.76472892510185, 1.87719762561912, 2.55263784794121, 2.08909646128974, 
  1.23409178557655, 1.14123164970623, 1.16824714048294, 1.33027594980446, 
  1.72123053551082, 1.59079093947271, 21.7875798026698, 56.9127173542448, 
  30.4252240218178, 31.3298868354637, 31.1716546537136, 32.2110476766607, 
  51.9341774935405, 61.2178911442429, 61.86586263717), growth_rate_adjusted = c(NA, 
  0.0488177278915325, -0.142243797340123, 0.25289291545105, -0.0875273116049197, 
  -0.037446297183831, 0.257247530652708, -0.0101941634203694, 0.208589963787459, 
  0.0959527069559234, 0.166295306767067, 0.161435622639821, 0.0300902645018133, 
  -0.105652999332261, -0.106690417306896, -0.0208119908185, -0.0320164222918163, 
  11.217767632108, 0.152595418413553, 0.287526446020315, 0.370531664639181, 
  -0.00908760744851222, -0.162811622080449, -0.259837161959523, 
  -0.108107297649273, 0.0138046383341219, 0.108237947135951, 0.156010823312755, 
  0.156247479548253, 0.264787314519994, 0.0548900361837266, -0.0884573534508115, 
  0.329789659376153, 0.123162250942545, 0.046595745924674, -0.193777861567299, 
  -0.11546565988949, 0.0312755235502229, -0.0742494534715727, 0.0753275693379953, 
  -0.671079629292128, NA, 0.200990405361887, 0.0792568872189554, 
  -0.00505432306108666, -0.122429074232749, -0.0184430716742389, 
  0.692655713093963, -0.0238745099660853, -0.00747435825830534, 
  0.0932061123918326, 16.7301360304552, 0.251014034652528, -0.304963611388244, 
  NA, 0.299236044468932, -0.0215671247266992, -0.212881719183966, 
  -0.0345567988624188, -0.57405185203053, -0.486290327620088, -0.218042277720125, 
  0.109466217082404, 0.0751856514127895, 0.417658424827343, -0.0583273025717256, 
  0.0147084656827421, -0.240398473456934, 0.242720456562112, 0.0976293990609434, 
  8.90548855887399, 0.186110513684643, 0.0584509559601001, -0.0133689396778527, 
  -0.0899370103640783, -0.102080935821528, -0.135771751035635, 
  -0.186104628327237, 4.99794204124976, -0.628117587552114, NA, 
  0.0989183973143732, 0.0637314314496069, 0.359813060225522, -0.181593086941546, 
  -0.40927007993941, -0.0752457288474235, 0.0236722235872695, 0.138693948999988, 
  0.293889839746277, -0.0757827573628338, 12.6960673222665, 1.61216334580085, 
  -0.465405529093955, NA, -0.00505051877720075, 0.0333441722774686, 
  0.61230947887394, 0.178759231372385, 0.0105846751793586)), row.names = c(NA, 
  -100L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))
  &gt; 
  </code></pre>