Skip to content
Back to formatted view

Raw Message

Message-ID: <20091111140425.25aef60c@bossiaea>
Date: 2009-11-11T06:04:25Z
From: Berwin A Turlach
Subject: Unexpected behaviour for as.date()
In-Reply-To: <CEEDLPBNOCMDHIGPLIJLKEBMCGAA.isabella@ghement.ca>

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