[R] Unexpected behaviour for as.date()

Berwin A Turlach berwin at maths.uwa.edu.au
Wed Nov 11 07:04:25 CET 2009


G'day Isabella,

On Tue, 10 Nov 2009 20:11:31 -0800
"Isabella Ghement" <isabella at ghement.ca> wrote:

> I tried the solution you suggested and find that am having problems
> getting R to extract the year from an object created by as.date():

Perhaps it would be best to first clarify what you really need. :)

If you have to extract the year, why go via as.date?  Why not just:

R> strsplit("02-MAY-00", "-")[[1]][3]
[1] "00"
R> as.numeric(strsplit("02-MAY-00", "-")[[1]][3])
[1] 0

Or if you have a vector of character strings, each a date in that
format:

R> x <- c("02-MAY-00", "02-MAY-01")
R> sapply(x, function(x) as.numeric(strsplit(x, "-")[[1]][3]))
02-MAY-00 02-MAY-01 
        0         1 

HTH.

Cheers,

	Berwin

========================== Full address ============================
Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)
School of Maths and Stats (M019)            +61 (8) 6488 3383 (self)
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway                   
Crawley WA 6009                e-mail: berwin at maths.uwa.edu.au
Australia                        http://www.maths.uwa.edu.au/~berwin




More information about the R-help mailing list