title: How to sum a function over a specific range in R?
body: <p>Here are three columns:</p>
  
  <pre><code>indx    vehID   LocalY
  1   2   35.381
  2   2   39.381
  3   2   43.381
  4   2   47.38
  5   2   51.381
  6   2   55.381
  7   2   59.381
  8   2   63.379
  9   2   67.383
  10  2   71.398
  11  2   75.401
  12  2   79.349
  13  2   83.233
  14  2   87.043
  15  2   90.829
  16  2   94.683
  17  2   98.611
  18  2   102.56
  19  2   106.385
  20  2   110.079
  21  2   113.628
  22  2   117.118
  23  2   120.6
  24  2   124.096
  25  2   127.597
  26  2   131.099
  27  2   134.595
  28  2   138.081
  29  2   141.578
  30  2   145.131
  31  2   148.784
  32  2   152.559
  33  2   156.449
  34  2   160.379
  35  2   164.277
  36  2   168.15
  37  2   172.044
  38  2   176
  39  2   179.959
  40  2   183.862
  41  2   187.716
  42  2   191.561
  43  2   195.455
  44  2   199.414
  45  2   203.417
  46  2   207.43
  47  2   211.431
  48  2   215.428
  49  2   219.427
  50  2   223.462
  51  2   227.422
  52  2   231.231
  53  2   235.001
  54  2   238.909
  55  2   242.958
  56  2   247.137
  57  2   251.247
  58  2   255.292
  59  2   259.31
  60  2   263.372
  61  2   267.54
  62  2   271.842
  63  2   276.256
  64  2   280.724
  65  2   285.172
  </code></pre>
  
  <p>I want to create a new column called 'Smoothed Y' by applying the following formula:
  <img src="https://i.stack.imgur.com/AZOYv.jpg" alt="enter image description here"></p>
  
  <p>D=15, delta (triangular symbol) = 5, i = indx, x_alpha(tk) = LocalY, x_alpha(ti) = smoothed value</p>
  
  <p>I have tried using following code for first calculating Z: (Kernel below means the exp function)</p>
  
  <pre><code>t &lt;- 0.5
  dt &lt;- 0.1
  delta &lt;- t/dt
  d &lt;- 3*delta
  indx &lt;- a$indx
  for (i in indx) {
  initial &lt;- i-d
  end &lt;- i+d
  k &lt;- c(initial:end)
  for (n in k) {
  kernel &lt;- exp(-abs(i-n)/delta)
  z &lt;- sum(kernel)
  }
  }
  a$z &lt;- z
  print (a)
  </code></pre>
  
  <p>NOTE: 'a' is the imported data frame containing the three columns above.</p>
  
  <p>Although the values of computed function are fine but it doesn't sum up the values in variable z. How can I do summation over the range i-d to i+d for every indx value i?</p>