<div dir="ltr">I do not understand why you have to make a list <span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:13px">Ref <- data[,list(Compare_Value=list(</span><span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:13px">I(Value)),Compare_Date=list(I(</span><span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:13px">Date)))] </span> when the data is already sitting in a data.table. Is it simply because lapply works on a list and not a data.table?</div>
<div class="gmail_extra"><br clear="all"><div>Farrel Buchinsky<br>Google Voice Tel: (412) 567-7870<br></div>
<br><br><div class="gmail_quote">On Mon, Jul 14, 2014 at 10:49 PM, Mike.Gahan <span dir="ltr"><<a href="mailto:michael.gahan@gmail.com" target="_blank">michael.gahan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
But what if the dates are irregularly spaced?<br>
<br>
#Build some sample data<br>
set.seed(12345)<br>
data <- data.table(Date=seq(1,60,by=3),Value=rpois(20,10))<br>
<div class=""><br>
#Build reference table. This is where we keep the list of Dates and Values<br>
that will be referenced for<br>
#each individual data<br>
Ref <- data[,list(Compare_Value=list(I(Value)),Compare_Date=list(I(Date)))]<br>
<br>
#Use lapply to get last seven days of value by id<br>
data[,Roll.Val := lapply(Date, function(x) {<br>
d <- as.numeric(Ref$Compare_Date[[1]] - x)<br>
sum((d <= 0 & d >= -7)*Ref$Compare_Value[[1]])})]<br>
<br>
head(data,10)<br>
<br>
Date Value Roll.Val<br>
</div> 1: 1 12 12<br>
2: 4 9 21<br>
3: 7 10 31<br>
4: 10 10 29<br>
5: 13 14 34<br>
6: 16 13 37<br>
7: 19 7 34<br>
8: 22 12 32<br>
9: 25 12 31<br>
10: 28 16 40<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://r.789695.n4.nabble.com/I-have-been-agnozing-over-how-to-do-a-running-cummulative-sum-over-a-particular-date-range-tp4693953p4694009.html" target="_blank">http://r.789695.n4.nabble.com/I-have-been-agnozing-over-how-to-do-a-running-cummulative-sum-over-a-particular-date-range-tp4693953p4694009.html</a><br>
<div class="HOEnZb"><div class="h5">Sent from the datatable-help mailing list archive at Nabble.com.<br>
_______________________________________________<br>
datatable-help mailing list<br>
<a href="mailto:datatable-help@lists.r-forge.r-project.org">datatable-help@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help</a><br>
</div></div></blockquote></div><br></div>