[R] Discretizing data rows into regular intervals

Gabor Grothendieck ggrothendieck at gmail.com
Sun Apr 3 11:39:15 CEST 2011


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



More information about the R-help mailing list