[R] Select range of dates

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 8 16:02:52 CEST 2006


And here is a fourth:

as.numeric(format(dd, "%Y")) + (quarters(dd) > "Q1")


On 10/8/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> Here are three alternative ways to get the fiscal year as a numeric
> value assuming:
> dd <- as.Date(x$Date,"%d/%m/%Y")
>
> # add one to year if month is past March
> as.numeric(format(dd, "%Y")) + (format(dd, "%m") > "03")
>
> # same but using POSIXlt
> # (Even though there are no time zones involved I have seen
> # situations where the time zone nevertheless crept in
> # where one would least expect it so even though I believe
> # this is correct I would nevertheless triple check
> # all your results if you use this one)
> with(as.POSIXlt(dd), 1900 + year + (mon > 2))
>
> # this makes use of the zoo yearqtr class
> # which represents dates as year + 0, .25, .5 or .75 for
> # the four quarters
> library(zoo)
> as.numeric(ceiling(as.yearqtr(dd)))
>
>
>
> On 10/7/06, Ian Broom <broomian at gmail.com> wrote:
> > Hello,
> >
> > This is likely fairly silly question, and I apologize to whomever takes the
> > time to respond.
> >
> > I am a relatively new user of R, on Windows XP, version 2.3.1.
> >
> > Say I have a data table that looks like the following:
> >
> > x
> >         Date Location  Amount  Blue Green
> > 1  01/01/2001  Central    1817  TRUE FALSE
> > 2  01/02/2001  Central   20358 FALSE  TRUE
> > 3  05/08/2001  Central   16245 FALSE  TRUE
> > 4  02/02/2002  Western     112  TRUE FALSE
> > 5  21/03/2002  Western   98756  TRUE FALSE
> > 6  01/04/2002  Western 1598414 FALSE  TRUE
> > 7  07/01/2001  Western    1255 FALSE  TRUE
> > 8  20/10/2003  Central   16289  TRUE FALSE
> > 9  21/10/2003  Eastern   10000 FALSE  TRUE
> > 10 22/10/2003  Eastern   98737 FALSE  TRUE
> > 11 23/10/2003  Eastern  198756  TRUE FALSE
> > 12 24/10/2003  Eastern   98756 FALSE  TRUE
> > 13 25/10/2003  Eastern   65895  TRUE FALSE
> > 14 26/10/2003  Eastern 2142266 FALSE  TRUE
> > 15 27/10/2003    North   98756  TRUE FALSE
> > 16 28/10/2003    North  548236 FALSE  TRUE
> >
> > and I want to do some summaries by Fiscal year (or FY quarter).
> > Reading manuals and such, I cobbled this less than satisfactory bit together
> > to start to build a factor representing a fiscal year split:
> >
> > y<-as.Date(x$Date,"%d/%m/%Y")
> > y<-as.matrix(y)
> > y$FY0203<-ifelse((y>=(as.Date("2002-03-21")))&(y<=(as.Date
> > ("2003-04-01"))),"TRUE","FALSE")
> >
> > the values seem correct, but aside from ugly, the data is not in the proper
> > format - with some more effort I might fix that... But my question is: is
> > there a more simple function available to select a range of dates? Or, at
> > least, a more elegant approach?
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>



More information about the R-help mailing list