title: Translating R to Rcpp: mean time difference between successive dates in vector
body: <p>I have an R function that takes a vector of sorted dates (descending order) that returns the mean time difference between successive dates in the vector. I am trying to translate this R function into an Rcpp function. </p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>sorted_dates &lt;- as.Date(c("2015-09-25", "2015-06-12",
                          "2015-06-12", "2015-03-26"))
  
  mean_time_difference &lt;- function(sorted_dates){
  ### Takes a vector of dates sorted in descending order
  ### Returns the mean time difference between dates.
  
  time_differences &lt;- integer()
  for(i in 1:(length(sorted_dates) - 1)){
  time_differences[i] &lt;- as.integer( sorted_dates[i] - sorted_dates[i+1])
  }
  
  return(mean(time_differences))
  
  }
  </code></pre>
  
  <p>This is my currently broken translation into Rcpp:</p>
  
  <pre><code>cppFunction('double mean_time_diff(DateVector sorted_dates) {
  /* Takes a vector of dates sorted in descending order
  */ Returns the mean time difference between dates. 
  
  int n = sorted_dates.size();
  IntegerVector time_diff;
  
  for(int i=1; i &lt; (n-1); i++){
    time_diff.push_back( sorted_dates[i] - sorted_dates[i+1] );
  }
  
  int m = time_diff.size();
  double total = 0; 
  for(int i=1; i &lt; m; i++) {
    total += time_diff[i];
  }
  
  return total / m;
  
  }')
  
  mean_time_difference(sorted_dates)
  mean_time_diff(sorted_dates)
  </code></pre>
  
  <p>I am sure there is plenty that could be improved in both the R and the Rcpp functions. Can anyone show me how to best implement that function in Rcpp? </p>