There still may be a problem if the dates go back far enough, e.g., 1909. Is '09' 1909 or 2009? No matter what, you have to decide which values need 1900 added and which need 2000. I'd split the date on the delimiter '.', decide whether to add 1900 or 2000, and then paste them together and then as.Date().

Clint

--
Clint Bowman                    INTERNET:       cl...@ecy.wa.gov
Air Quality Modeler             INTERNET:       cl...@math.utah.edu
Department of Ecology           VOICE:          (360) 407-6815
PO Box 47600                    FAX:            (360) 407-7534
Olympia, WA 98504-7600


        USPS:           PO Box 47600, Olympia, WA 98504-7600
        Parcels:        300 Desmond Drive, Lacey, WA 98503-1274


On Fri, 10 Dec 2010, Barry Rowlingson wrote:

On Fri, Dec 10, 2010 at 3:27 PM, Daniel Brewer <daniel.bre...@icr.ac.uk> wrote:
Hello,

I have some data that has dates in the form 27.02.37.  I convert them to
a date object as follows:
as.Date(data$date,format="%d.%m.%y")

But this gives me years such as 2037 when I would like them to be 1937.
 I thought of trying to take off some time i.e.
as.Date(camCD$DoB,format="%d.%m.%y") - 100*365
But that doesn't seem to work out correctly.  Any ideas how to do this?

Normally to adjust dates you can use as.difftime() and do arithmetic,
but a year is a variable thing (can be 365 or 366 days) so you cant
make a difftime of years. Days are variable things if you worry about
leap seconds...

Also, you could end up with an invalid date if you have 29-Feb-2000
and 29-Feb-1900. One wasn't a leap year...

A solution minus those caveats is to convert to POSIXlt and adjust the
$year element:

> dob="27.02.37"
> as.Date(dob,format="%d.%m.%y")
[1] "2037-02-27"
> dobp = as.POSIXlt(as.Date(dob,format="%d.%m.%y"))
> dobp$year = dobp$year - 100

> dobp
[1] "1937-02-27 UTC"
> as.Date(dobp)
[1] "1937-02-27"

although it might be easier to paste a '19' into your character variable

> paste(substr(dob,1,6),"19",substr(dob,7,9),sep="")
[1] "27.02.1937"

and then do it the way you started. Assumes you have leading zeroes on
all fields though.

Barry

______________________________________________
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.

Reply via email to