[R] why there is no quarters?

Marc Schwartz marc_schwartz at me.com
Mon Dec 16 14:12:12 CET 2013


That will only work if your starting date happens to be the first day of the year:

x <- seq(as.Date("2001/1/1"), as.Date("2010/1/1"), "3 months")

> head(x)
[1] "2001-01-01" "2001-04-01" "2001-07-01" "2001-10-01" "2002-01-01"
[6] "2002-04-01"


Compare that to:

x2 <- seq(as.Date("2001/2/3"), as.Date("2010/1/1"), "3 months")

> head(x2, 10)
 [1] "2001-02-03" "2001-05-03" "2001-08-03" "2001-11-03" "2002-02-03"
 [6] "2002-05-03" "2002-08-03" "2002-11-03" "2003-02-03" "2003-05-03"


The "3 months" is literally 3 months from the defined start date, not 3 months from the first of the year. So you are not going to get calendar quarter starting dates in that case.


On the other hand:

> cut(x2, breaks = "quarter")
 [1] 2001-01-01 2001-04-01 2001-07-01 2001-10-01 2002-01-01 2002-04-01
 [7] 2002-07-01 2002-10-01 2003-01-01 2003-04-01 2003-07-01 2003-10-01
[13] 2004-01-01 2004-04-01 2004-07-01 2004-10-01 2005-01-01 2005-04-01
[19] 2005-07-01 2005-10-01 2006-01-01 2006-04-01 2006-07-01 2006-10-01
[25] 2007-01-01 2007-04-01 2007-07-01 2007-10-01 2008-01-01 2008-04-01
[31] 2008-07-01 2008-10-01 2009-01-01 2009-04-01 2009-07-01 2009-10-01
36 Levels: 2001-01-01 2001-04-01 2001-07-01 2001-10-01 ... 2009-10-01


Regards,

Marc Schwartz


On Dec 16, 2013, at 6:35 AM, Dániel Kehl <kehld at ktk.pte.hu> wrote:

> Hi,
> 
> try
> 
> x <- seq(as.Date("2001/1/1"),as.Date("2010/1/1"),"3 months")
> 
> best,
> daniel
> ________________________________________
> Feladó: r-help-bounces at r-project.org [r-help-bounces at r-project.org] ; meghatalmazó: Pancho Mulongeni [p.mulongeni at namibia.pharmaccess.org]
> Küldve: 2013. december 16. 13:05
> To: 1248283536 at qq.com
> Cc: r-help at r-project.org
> Tárgy: Re: [R] why there is no quarters?
> 
> Hi,
> I also would like to use quarters. I think a work around would be to just label each record in the dataframe by its quarter.
> i.e. you add a factor called 'Quarter' with four levels (Q1 to Q4) for each row and you assign the level based on the month of the date.
> You can easily do this with as.Date and as.character.
> 
> Pancho Mulongeni
> Research Assistant
> PharmAccess Foundation
> 1 Fouché Street
> Windhoek West
> Windhoek
> Namibia
> 
> Tel:   +264 61 419 000
> Fax:  +264 61 419 001/2
> Mob: +264 81 4456 286



More information about the R-help mailing list