Hi guys, I'd like to thank you ahead of time for any help that you can offer me. I'm kind of stuck trying to do this. I have a data frame with dates and values (note: only two columns shown): head(test) date value stop 1 01/02/05 100 12/01/07 2 07/16/05 200 12/01/07 3 12/20/05 150 12/01/07 4 04/01/06 250 12/01/07 5 10/01/06 10 12/01/07 What I need to do is create regularly spaced 3-month intervals (starting with the first observed date) with values that are closest to but recorded after the date created. I would stop at the stop date. So the result would look like: new_date value 1 01/02/05 100 2 04/02/05 100 3 07/02/05 100 4 10/02/05 200 5 01/02/06 150 6 04/02/06 250 7 07/02/06 250 8 10/02/06 10 9 01/02/07 10 etc etc etc 10/02/07 --- ## Final obs since next one would be 1/2/08 (after stop date) Again, I would be extremely grateful for any help. Thanks, -linh
The ?cut function should work for that. Have you tried? D. -- View this message in context: http://r.789695.n4.nabble.com/Discretizing-data-rows-into-regular-intervals-tp3422921p3422933.html Sent from the R help mailing list archive at Nabble.com.
Sorry, I did not get the question because I read it too sloppily. I hope this is not homework. You can proceed along this example: set.seed(32345) #Value of observation value=rpois(60,100) #Day of observation day=sample(1:1080,50,replace=F) day=sort(day) #Assume 3 years #Assume months have all 30 days #For real dates, the breaks in cut() #have to be defined properly all.days=seq(from=1,to=1080) quarter=cut(all.days,breaks=12) quarter=as.factor(as.numeric(quarter)) #In which quarter is a certain observation quarter.of.day=quarter[day] #What's the minimum day in a quarter min.day=tapply(day,quarter.of.day,min) #What's the value at that day values.at.min.day=value[which(day%in%min.day)] hth, daniel -- View this message in context: http://r.789695.n4.nabble.com/Discretizing-data-rows-into-regular-intervals-tp3422921p3422975.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2011-Apr-03 09:39 UTC
[R] Discretizing data rows into regular intervals
On Sat, Apr 2, 2011 at 9:31 PM, Linh Tran <Tranlm at berkeley.edu> wrote:> Hi guys, > > I'd like to thank you ahead of time for any help that you can offer me. > I'm kind of stuck trying to do this. > > I have a data frame with dates and values (note: only two columns shown): > > head(test) > ? ? ? ?date ? ? value ? ? ? ? stop > 1 ? ? 01/02/05 ? ? 100 ? ? 12/01/07 > 2 ? ? 07/16/05 ? ? 200 ? ? 12/01/07 > 3 ? ? 12/20/05 ? ? 150 ? ? 12/01/07 > 4 ? ? 04/01/06 ? ? 250 ? ? 12/01/07 > 5 ? ? 10/01/06 ? ? ?10 ? ? 12/01/07 > > What I need to do is create regularly spaced 3-month intervals (starting > with the first observed date) with values that are closest to but recorded > after the date created. I would stop at the stop date. So the result would > look like: > > ? ? ?new_date ? value > 1 ? ? 01/02/05 ? ? 100 > 2 ? ? 04/02/05 ? ? 100 > 3 ? ? 07/02/05 ? ? 100 > 4 ? ? 10/02/05 ? ? 200 > 5 ? ? 01/02/06 ? ? 150 > 6 ? ? 04/02/06 ? ? 250 > 7 ? ? 07/02/06 ? ? 250 > 8 ? ? 10/02/06 ? ? ?10 > 9 ? ? 01/02/07 ? ? ?10 > etc > etc > etc ? 10/02/07 ? ? --- ?## Final obs since next one would be 1/2/08 (after > stop date) >See question #13 in the zoo-faq vignette: http://cran.r-project.org/web/packages/zoo/index.html and note the existence of zoo's yearqtr class. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com