[R] Remove 100 years from a date object
b.rowlingson at lancaster.ac.uk
Fri Dec 10 17:17:42 CET 2010
On Fri, Dec 10, 2010 at 3:27 PM, Daniel Brewer <daniel.brewer at icr.ac.uk> wrote:
> I have some data that has dates in the form 27.02.37. I convert them to
> a date object as follows:
> 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
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
> dobp = as.POSIXlt(as.Date(dob,format="%d.%m.%y"))
> dobp$year = dobp$year - 100
 "1937-02-27 UTC"
although it might be easier to paste a '19' into your character variable
and then do it the way you started. Assumes you have leading zeroes on
all fields though.
More information about the R-help