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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] På vegne af Arun Kumar Saha
Sendt: 18. september 2007 11:01
Til: [EMAIL PROTECTED]
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@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@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,                  [EMAIL PROTECTED]
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
______________________________________________
R-help@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.

Reply via email to