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? Thanks Dan -- ************************************************************** Daniel Brewer, Ph.D. Institute of Cancer Research Molecular Carcinogenesis Email: daniel.brewer at icr.ac.uk ************************************************************** The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP. This e-mail message is confidential and for use by the a...{{dropped:2}}
On Fri, Dec 10, 2010 at 3:27 PM, Daniel Brewer <daniel.brewer at 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
On Fri, Dec 10, 2010 at 10:27 AM, Daniel Brewer <daniel.brewer at 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? >The easiest is just to use chron dates since it uses a cut.off of 30 by default. That is, if yy is less than that then 2000+yy is used and if greater than that then 1900+yy is used. Thus try this: library(chron) d <- "27.02.37" as.Date(dates(d, format = "d.m.y")) # "1937-02-27" as.Date(d, format = "%d.%m.%y") # "2037-02-27" Also if that is not good enough and you want a different value for the cut.off then note that the default in chron is to use the year.expand function to expand two digit dates but you can change that via something like this: options(chron.year.expand = function(..., cut.off = 25) year.expand(..., cut.off = cut.off)) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Maybe Matching Threads
- is there a packge or code to generate markov chains in R
- Calculate difference between dates in years
- Collapse data matrix with extra info separated by commas
- Stacked barplot with two stacked bars besides each other
- Find classes for each column of a data.frame