title: Vectorised way to calculate mean of left and right neighbours in a vector
body: <p>I have a vector </p>
  
  <pre><code>x = c(1820.0, 2235.0, 2534.0, 2580.0, 2322.0, 2317.0, 2331.0, 2345.0, 
        2305.0, 2265.0, 2277.0, 2289.0, 2338.0, 2387.0, 2152.0, 2256.0, 
        2360.0, 2590.0, 2529.0, 2468.0, 2776.0, 2909.0, 3017.0, 3081.0,
        3118.5, 3156.0, 3338.0, 3211.5)
  </code></pre>
  
  <p>I want to calculate the mean of left and right neighbors of each element except edges. For example, the result should be like this :  <code>mean(1820,2534), mean(2235,2580), mean(2534,2322) ...</code></p>
  
  <p>I am able to do this using loops but that is very slow. I need a vectorized solution.</p>
  
  <p>My code using a for loop:</p>
  
  <pre><code>neighbour_m = function(x) {
    newx = c(x[length(x)], x, x[1])
    for (i in 2:(length(newx) - 1)){
      m = mean(c(newx[i-1], newx[i+1]))
    }
  }
  </code></pre>