Thank you for your kind reply. Let me add some more substance to the problem at hand. My workflow is in entirely in R/dplyr. A certain logic of the code needs extensive look-back & iteration - to speed up the code, I am trying to write that piece of logic in Rcpp. Here's an hack attempt with a sample data-set and a bit of pseudocode to give you a slightly better idea.

library(readr);

library(dplyr);

# Data frame df with Symbol, Date & VWAP.

structure(list(Symbol = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,

1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A",

"B"), class = "factor"), Date = structure(c(9L, 7L, 5L, 6L, 8L,

4L, 3L, 1L, 2L, 9L, 8L, 7L, 5L, 6L, 4L, 3L, 1L, 2L), .Label = c("2020-01-21",

"2020-01-22", "2020-01-23", "2020-01-24", "2020-01-28", "2020-01-29",

"2020-01-30", "2020-01-31", "2020-02-03"), class = "factor"),

VWAP = c(38.25, 39, 39.1, 39.34, 39.4, 40.45, 41.08, 41.2,

41.21, 96.86, 98.77, 99.19, 99.99, 100.43, 103.18, 106.19,

106.45, 107.38)), class = "data.frame", row.names = c(NA,

-18L))

# Rcpp function with logic to:

```
1. Uniquely subset the dataframe by Symbol A & B (already solved).
2. Sort the subset dataframe by Date (attempting to solve and the main question of this post)
3. Loop thru VWAP to calculate the variables of interest for various date range
4. Return the aggregate dataframe back to R
```

Regards