Todd Young - 24 days ago 8

C++ Question

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.

Here is what I have so far:

`sorted_dates <- as.Date(c("2015-09-25", "2015-06-12",`

"2015-06-12", "2015-03-26"))

mean_time_difference <- function(sorted_dates){

### Takes a vector of dates sorted in descending order

### Returns the mean time difference between dates.

time_differences <- integer()

for(i in 1:(length(sorted_dates) - 1)){

time_differences[i] <- as.integer( sorted_dates[i] - sorted_dates[i+1])

}

return(mean(time_differences))

}

This is my currently broken translation into Rcpp:

`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 < (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 < m; i++) {

total += time_diff[i];

}

return total / m;

}')

mean_time_difference(sorted_dates)

mean_time_diff(sorted_dates)

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?

Answer

Is this what you are looking for, in a plain R approach:

```
> sorted_dates <- as.Date(c("2015-09-25", "2015-06-12",
+ "2015-06-12", "2015-03-26"))
> mean(diff(sorted_dates))
Time difference of -61 days
> mean(as.numeric(diff(sorted_dates)))
[1] -61
>
```

You *can* do these things with Rcpp, but you probably do them in base R, or with any of the add-on aggregation utilities -- I like data.table.

Source (Stackoverflow)

Comments