Re: [R] day, month, year functions

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri 11 Aug 2006 - 09:22:14 EST

Here are three ways:

xx <- as.Date("2006-01-05")

# 1. use as.POSIXlt

as.POSIXlt(xx)$mday
as.POSIXlt(xx)$mon + 1
as.POSIXlt(xx)$year + 1900

# 2. use format

as.numeric(format(xx, "%d"))
as.numeric(format(xx, "%m"))
as.numeric(format(xx, "%Y"))

# 3. use month.day.year in chron package library(chron)

month.day.year(unclass(xx))$day
month.day.year(unclass(xx))$month
month.day.year(unclass(xx))$year

Also see the help desk article in R News 4/1.

On 8/10/06, Horace Tso <Horace.Tso@pgn.com> wrote:
> Hi list,
>
> I'm trying to turn a date into something productive. (Not what you may be thinking....)
>
> I want three functions so I could take a "date" object and get the day of week, month, and year from it.
>
> xx <- as.Date("2006-01-05")
>
> month(xx) equal 1
> day(xx) equal 5
> year(xx) equal 2006
>
> I'm aware of the weekdays() and months() functions in the base package. But they return a character object which requires some coding to convert into a numeric value.
>
> I've also tried the sday.of.week() in fCalendar but it doesn't like my date,
>
> > sday.of.week(xx)
> Error in Ops.Date(sdates, 10000) : %/% not defined for Date objects
>
> Do these functions exist in some package I'm not aware of?
>
> Thanks in adv.
>
> Horace Tso



R-help@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. Received on Fri Aug 11 09:26:24 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 11 Aug 2006 - 12:30:47 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.