Hello, I can't figure out a "proper" way to extract the month number from a Date class variable. I can do it like this:> today <- as.Date(Sys.time()) > as.integer (format.Date (today, "%m"))but it must be inefficient, since I convert a date to text and back to a number. I tried: > months(today) but the months() function returns the name of the month, not the month number. I need to process some time series of station data using season-dependent criteria, so I need the months numerically. Thanks in advance for any help, Maciej.
Maciej Radziejewski <maciejr_copies at yahoo.com> writes:> Hello, > > I can't figure out a "proper" way to extract the month number from a Date class variable. I can do it like this: > > > today <- as.Date(Sys.time()) > > as.integer (format.Date (today, "%m")) > > but it must be inefficient, since I convert a date to text and back to a number. I tried: > > > months(today) > > but the months() function returns the name of the month, not the month number. I need to process some time series of station data using season-dependent criteria, so I need the months numerically. > > Thanks in advance for any help,Wrong class, Date (and POSIXct) objects are basically numeric (time in days, resp. seconds, since Jan. 1, 1970):> structure(1,class="Date")[1] "1970-01-02"> structure(1,class="POSIXct")[1] "1970-01-01 01:00:01 CET" Instead, try:> as.POSIXlt(Sys.time())$mon[1] 5 (NB: zero-based...) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Hi you can also do as.POSIXlt(today)$mon but I do not know if it is more efficient HTH Petr On 20 Jun 2006 at 5:20, Maciej Radziejewski wrote: Date sent: Tue, 20 Jun 2006 05:20:38 -0700 (PDT) From: Maciej Radziejewski <maciejr_copies at yahoo.com> To: r-help at stat.math.ethz.ch Subject: [R] Accessing date subfields Send reply to: Maciej Radziejewski <maciejr_copies at yahoo.com> <mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe> <mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>> Hello, > > I can't figure out a "proper" way to extract the month number from a > Date class variable. I can do it like this: > > > today <- as.Date(Sys.time()) > > as.integer (format.Date (today, "%m")) > > but it must be inefficient, since I convert a date to text and back to > a number. I tried: > > > months(today) > > but the months() function returns the name of the month, not the month > number. I need to process some time series of station data using > season-dependent criteria, so I need the months numerically. > > Thanks in advance for any help, > > Maciej. > > ______________________________________________ > 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.htmlPetr Pikal petr.pikal at precheza.cz