Hi R-users, What is the best way to achieve a table which contains all days and months between years 2007-2020? I would like to calculate number of days in each month within those years (to data frame). Regards, Lauri [[alternative HTML version deleted]]
> diff(seq(as.Date("2007-01-01"), as.Date("2021-01-01"), by = "month"))Time differences in days [1] 31 28 31 30 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 31 [26] 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 [51] 31 30 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 [76] 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 [101] 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 [126] 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 [151] 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 On 8/7/07, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:> Hi R-users, > > > > What is the best way to achieve a table which contains all days and months > between years 2007-2020? I would like to calculate number of days in each > month within those years (to data frame). > > > > Regards, > > Lauri > > [[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. >
On Tue, Aug 07, 2007 at 03:53:28PM +0300, Lauri Nikkinen wrote:> What is the best way to achieve a table which contains all days and months > between years 2007-2020? I would like to calculate number of days in each > month within those years (to data frame).Here you go -- just replace length.out=3 by 24*12 for your twentyfour years: > as.POSIXlt(seq(as.Date("2007-02-01"), by="month", length.out=3)-1)$mday [1] 31 28 31 > A sequence of month at the first of the next month, shifted back a day gives the last one of that month -- converted to POSIXlt from which we extract the day-of-the-month. Hth, Dirk -- Three out of two people have difficulties with fractions.
Lauri Nikkinen wrote:> Hi R-users, > > What is the best way to achieve a table which contains all days and months > between years 2007-2020? I would like to calculate number of days in each > month within those years (to data frame). > > Regards, > > LauriHow about this? X <- seq(ISOdate(2007,1,1), ISOdate(2020,12,31), by=60*60*24) mytab <- table(substring(X, 3, 4), substring(X, 6, 7)) mytab 01 02 03 04 05 06 07 08 09 10 11 12 07 31 28 31 30 31 30 31 31 30 31 30 31 08 31 29 31 30 31 30 31 31 30 31 30 31 09 31 28 31 30 31 30 31 31 30 31 30 31 10 31 28 31 30 31 30 31 31 30 31 30 31 11 31 28 31 30 31 30 31 31 30 31 30 31 12 31 29 31 30 31 30 31 31 30 31 30 31 13 31 28 31 30 31 30 31 31 30 31 30 31 14 31 28 31 30 31 30 31 31 30 31 30 31 15 31 28 31 30 31 30 31 31 30 31 30 31 16 31 29 31 30 31 30 31 31 30 31 30 31 17 31 28 31 30 31 30 31 31 30 31 30 31 18 31 28 31 30 31 30 31 31 30 31 30 31 19 31 28 31 30 31 30 31 31 30 31 30 31 20 31 29 31 30 31 30 31 31 30 31 30 31 head(as.data.frame(mytab)) Var1 Var2 Freq 1 07 01 31 2 08 01 31 3 09 01 31 4 10 01 31 5 11 01 31 6 12 01 31> [[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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Just one refinement if you want it nicely formatted:> ts(diff(seq(as.Date("2007-01-01"), as.Date("2021-01-01"), by = "month")), start = c(2007, 01), freq = 12)Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 2007 31 28 31 30 31 30 31 31 30 31 30 31 2008 31 29 31 30 31 30 31 31 30 31 30 31 2009 31 28 31 30 31 30 31 31 30 31 30 31 2010 31 28 31 30 31 30 31 31 30 31 30 31 2011 31 28 31 30 31 30 31 31 30 31 30 31 2012 31 29 31 30 31 30 31 31 30 31 30 31 2013 31 28 31 30 31 30 31 31 30 31 30 31 2014 31 28 31 30 31 30 31 31 30 31 30 31 2015 31 28 31 30 31 30 31 31 30 31 30 31 2016 31 29 31 30 31 30 31 31 30 31 30 31 2017 31 28 31 30 31 30 31 31 30 31 30 31 2018 31 28 31 30 31 30 31 31 30 31 30 31 2019 31 28 31 30 31 30 31 31 30 31 30 31 2020 31 29 31 30 31 30 31 31 30 31 30 31 On 8/7/07, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> > diff(seq(as.Date("2007-01-01"), as.Date("2021-01-01"), by = "month")) > Time differences in days > [1] 31 28 31 30 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 31 > [26] 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 > [51] 31 30 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 > [76] 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 > [101] 31 30 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 > [126] 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 > [151] 31 31 30 31 30 31 31 29 31 30 31 30 31 31 30 31 30 31 > > > On 8/7/07, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote: > > Hi R-users, > > > > > > > > What is the best way to achieve a table which contains all days and months > > between years 2007-2020? I would like to calculate number of days in each > > month within those years (to data frame). > > > > > > > > Regards, > > > > Lauri > > > > [[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. > > >