[R] Need help on "date"

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Sep 18 14:01:49 CEST 2007


On Tue, 18 Sep 2007, Frede Aakmann Tøgersen wrote:

> One way (perhaps not the most efficient)
>
>> as.Date("2005-09-01","%Y-%m-%d")
> [1] "2005-09-01"
>> format(as.Date("2005-09-01","%Y-%m-%d"),"%Y")
> [1] "2005"
>> format(as.Date("2005-09-01","%Y-%m-%d"),"%d")
> [1] "01"
>> format(as.Date("2005-09-01","%Y-%m-%d"),"%m")
> [1] "09"

It's pretty efficient, but should you want numeric (rather than character) 
answers

> zz <- strptime("2005-09-01","%Y-%m-%d")
> zz$year + 1900
[1] 2005
> zz$mon + 1
[1] 9
> zz$mday
[1] 1

(I'm not sure why the POSIX people chose inconsistent origins, but they 
did.)

>>
>
>
> See ?DateTimeClasses.
>
> Med venlig hilsen
> Frede Aakmann Tøgersen
>
>
>
>
>> -----Oprindelig meddelelse-----
>> Fra: r-help-bounces at r-project.org
>> [mailto:r-help-bounces at r-project.org] På vegne af Arun Kumar Saha
>> Sendt: 18. september 2007 11:01
>> Til: r-help at stat.math.ethz.ch
>> Emne: [R] Need help on "date"
>>
>> Dear all,
>>
>> I have a variable 'x' like that:
>>
>>> x
>> [1] "2005-09-01"
>>
>> Here, 2005 represents year, 09 month and 01 day.
>>
>> Now I want to create three variables naming: y, m, and d such that:
>>
>> y = 2005
>> m = 09
>> d = 01
>>
>> can anyone tell me how to do that?
>>
>> Regards,
>>
>> 	[[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>>
>
> ______________________________________________
> R-help at r-project.org 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.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


More information about the R-help mailing list