Take a daily rolling mean of a seven day window for 30 minute sampled data
<p>I would like to take a mean of a 7 day rolling window with 1 day increments of data that is collected at 30 minute intervals.
I have tried using <code>data.table</code> with <code>by</code> conditional statement with no success. Any guidane would be greatly appreciated.</p>

<pre><code># packages
library(data.table)
library(lubridate)

# Set set.seed to have reproducible sampling 
set.seed(42)

# Create some Data
start = ymd_hms("2014-01-01 00:00:00")
end = ymd_hms("2014-12-31 23:59:59")

# Create data with 30 minute intervals.
dat &lt;- data.table(timestamp = seq(start, end, by = "30 min"),
                  sample1 = sample(1:20, 17520, replace = TRUE))

# Create date variable for merging datasets.
dat[, date := as.Date(timestamp)]

# Create data for 7 day window moving window with one day increments.
dat2 &lt;- data.table(start = seq(start, end, by = "1 day"),
                  end = seq(start + days(7), end + days(7), by = "1 day"))

# Create date variable for merging datasets.
dat2[, date := as.Date(start)]

# mergre datasets.
dat &lt;- merge(dat, dat2, by="date")

# Tried 
dat[, .(sample.mean = mean(sample1)), by = .(timestamp &gt;= start &amp; timestamp &lt; end)]
#    timestamp sample.mean
# 1:      TRUE    10.46638

dat[, .(sample.mean = mean(sample1)), by = .(timestamp %in% c(start:end))]
#    timestamp sample.mean
# 1:      TRUE    10.40059
# 2:     FALSE    10.46767
#  Warning messages:
# 1: In start:end :
#  numerical expression has 17520 elements: only the first used
# 2: In start:end :
#   numerical expression has 17520 elements: only the first used

dat[, .(sample.mean = mean(sample1)), by = .(timestamp %between% c(start, end))]
#    timestamp sample.mean
# 1:      TRUE    19.00000
# 2:     FALSE    10.46589
</code></pre>