title: Efficient method to smooth data in R
body: <p>This question is related to another question which I asked <a href="https://stackoverflow.com/questions/19236949/how-to-sum-a-function-over-a-specific-range-in-r">here</a>.</p>
  <h1>Data</h1>
  <p>My data frame contains velocities and accelerations of vehicles in every 0.1 seconds time frames. Following shows very few data points of the data frame which originally contains more than 1,000,000 rows:</p>
  <pre><code>&gt; dput(head(traj2[, c(1,2,12,13)], 30))
  structure(list(Vehicle.ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L), Frame.ID = 270:299, Vehicle.velocity = c(19.89, 
  19.89, 19.89, 19.89, 19.89, 19.97, 20, 19.86, 19.18, 18.17, 17.63, 
  17.87, 18.76, 19.67, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
  20, 20, 20, 20, 19.99, 19.98), Vehicle.acceleration = c(0, 0, 
  0, 0, 1.07, 0.6, 0, -2.42, -9.79, -11.2, -2.64, 9.2, 11.2, 5.32, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.11, -0.2)), .Names = c(&quot;Vehicle.ID&quot;, 
  &quot;Frame.ID&quot;, &quot;Vehicle.velocity&quot;, &quot;Vehicle.acceleration&quot;), row.names = c(NA, 
  30L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>Please note that there are about 2000 unique <code>Vehicle.ID</code>s, each repeating in different number of frames.</p>
  <h1>Goal</h1>
  <p>I want to smooth the velocities and accelerations by using the equation provided in the first question. But I want to minimize the time and make the code as efficient as possible.</p>
  <h1>What I have Tried</h1>
  <p>I used following code for smoothing:</p>
  <pre><code>#  VELOCITY SMOOTHING FUNCTION
  smooth &lt;- function (x, D, delta){
  z &lt;- exp(-abs(-D:D/delta))
  r &lt;- convolve (x, z, type='filter')/convolve(rep(1, length(x)),z,type='filter')
  r
  }
  #  ACCELERATION SMOOTHING FUNCTION
  smootha &lt;- function (x, D, delta){
    za &lt;- exp(-abs(-D:D/delta))
    ra &lt;- convolve (x, za, type='filter')/convolve(rep(1, length(x)),za,type='filter')
    ra
  } ### D=3*delta = 3*40 = 120, delta for acceleration = T / dt = 4 seconds/0.1 = 40
  ftaa &lt;- list()
  
  
   # Split data by vehicle ID
      length(ftaa) &lt;- length(unique(traj1$'Vehicle.ID'))
  
  # Apply smoothing function
  for (i in 1:length(unique(traj2$'Vehicle.ID'))){
    veh &lt;- subset (traj2, traj2$'Vehicle.ID'==unique(traj2$'Vehicle.ID')[i])
    svel &lt;- round(smooth(veh$'Vehicle.velocity',30,10), digits=2)
    svel &lt;- data.frame(svel)
    svel &lt;- head(tail(svel,-90),-90)
    sacc &lt;- round(smootha(veh$'Vehicle.acceleration',120,40), digits=2)
    sacc &lt;- data.frame(sacc)
    veh &lt;- head(tail(veh, -120), -120)
    ftaa[[i]] &lt;- cbind(veh,svel,sacc)
  }
  
  # Combining results
  final.data1&lt;-do.call(&quot;rbind&quot;, ftaa)
  </code></pre>
  <h1>The Problem</h1>
  <p>This approach takes about 40 minutes to complete smoothing the given data frame. Is there any way I could reduce this time?</p>