This "reference" could be done in the actual data.table, but I wanted to avoid it due to size concerns. In the event that we want to do rolling calls by groups, we could have a lot of redundant data inside the data.table. This redundant data could possibly bloat the data to be VERY VERY large if we are not careful. A separate table helps to alleviate this problem. This is especially true in this case (where we implicitly only have 1 group).
On Monday, July 14, 2014, Farrel Buchinsky <[email protected]> wrote: > I do not understand why you have to make a list Ref <- > data[,list(Compare_Value=list(I(Value)),Compare_Date=list(I(Date)))] when > the data is already sitting in a data.table. Is it simply because lapply > works on a list and not a data.table? > > Farrel Buchinsky > Google Voice Tel: (412) 567-7870 > > > On Mon, Jul 14, 2014 at 10:49 PM, Mike.Gahan <[email protected] > <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote: > >> But what if the dates are irregularly spaced? >> >> #Build some sample data >> set.seed(12345) >> data <- data.table(Date=seq(1,60,by=3),Value=rpois(20,10)) >> >> #Build reference table. This is where we keep the list of Dates and >> Values >> that will be referenced for >> #each individual data >> Ref <- >> data[,list(Compare_Value=list(I(Value)),Compare_Date=list(I(Date)))] >> >> #Use lapply to get last seven days of value by id >> data[,Roll.Val := lapply(Date, function(x) { >> d <- as.numeric(Ref$Compare_Date[[1]] - x) >> sum((d <= 0 & d >= -7)*Ref$Compare_Value[[1]])})] >> >> head(data,10) >> >> Date Value Roll.Val >> 1: 1 12 12 >> 2: 4 9 21 >> 3: 7 10 31 >> 4: 10 10 29 >> 5: 13 14 34 >> 6: 16 13 37 >> 7: 19 7 34 >> 8: 22 12 32 >> 9: 25 12 31 >> 10: 28 16 40 >> >> >> >> -- >> View this message in context: >> 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 >> Sent from the datatable-help mailing list archive at Nabble.com. >> _______________________________________________ >> datatable-help mailing list >> [email protected] >> <javascript:_e(%7B%7D,'cvml','[email protected]');> >> >> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help >> > >
_______________________________________________ datatable-help mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
