Hi R-friends, Can anyone explain the following strange behavior to me?> as.Date( "4/25/71","%m/%d/%y")[1] "1971-04-25"> as.Date( "4/25/62","%m/%d/%y")[1] "2062-04-25" so 71 is converted to 1971, while 62 is converted to 2062? Does anyone know why? And is there a simple way to specify the date? (does works the same way in R 2.01 as well as in 1.9) ------------------- dr F.H.G. (Frans) Marcelissen DigiPsy (www.DigiPsy.nl <http://www.digipsy.nl/>) Pomperschans 26 5595 AV Leende tel: 040 2065030/06 2325 06 53 skype adres: frans.marcelissen email: frans.marcelissen@digipsy.nl [[alternative HTML version deleted]]
Maybe there is some built-in assumption about dating and when to change to the new century. It seems to kick-in at 1968 as.Date("25/04/69", "%d/%m/%y") as.Date("25/04/68", "%d/%m/%y") as.Date("25/04/60", "%d/%m/%y") John Kane Kingston ON Canada> -----Original Message----- > From: frans.marcelissen at digipsy.nl > Sent: Fri, 30 Aug 2013 15:12:40 +0200 > To: r-help at r-project.org > Subject: [R] strange conversion char to date > > Hi R-friends, > Can anyone explain the following strange behavior to me? >> as.Date( "4/25/71","%m/%d/%y") > [1] "1971-04-25" >> as.Date( "4/25/62","%m/%d/%y") > [1] "2062-04-25" > > so 71 is converted to 1971, while 62 is converted to 2062? Does anyone > know > why? And is there a simple way to specify the date? > (does works the same way in R 2.01 as well as in 1.9) > > ------------------- > dr F.H.G. (Frans) Marcelissen > DigiPsy (www.DigiPsy.nl <http://www.digipsy.nl/>) > Pomperschans 26 > 5595 AV Leende > tel: 040 2065030/06 2325 06 53 > skype adres: frans.marcelissen > email: frans.marcelissen at digipsy.nl > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM?, MSN? Messenger, Yahoo!? Messenger, ICQ?, Google Talk? and most webmails
Hi, You may try: x1<- c("4/25/71","4/20/64") fun1<- function(x, year){ m1<-as.numeric(format(as.Date(x1,"%m/%d/%y"),"%y")) m1<- ifelse(m1>year%%100,1900+m1,2000+m1) m2<- paste0(gsub("(.*\\/).*$","\\1",x),m1) as.Date(m2,"%m/%d/%Y") } fun1(x1,1950) #[1] "1971-04-25" "1964-04-20" ?str(fun1(x1,1950)) # Date[1:2], format: "1971-04-25" "1964-04-20" A.K. ----- Original Message ----- From: Frans Marcelissen <frans.marcelissen at digipsy.nl> To: "R-help at r-project.org" <R-help at r-project.org> Cc: Sent: Friday, August 30, 2013 9:12 AM Subject: [R] strange conversion char to date Hi R-friends, Can anyone explain the following strange behavior to me?> as.Date( "4/25/71","%m/%d/%y")[1] "1971-04-25"> as.Date( "4/25/62","%m/%d/%y")[1] "2062-04-25" so 71 is converted to 1971, while 62 is converted to 2062? Does anyone know why? And is there a simple way to specify the date? (does works the same way in R 2.01 as well as in 1.9) ------------------- dr F.H.G. (Frans) Marcelissen DigiPsy (www.DigiPsy.nl <http://www.digipsy.nl/>) Pomperschans 26 5595 AV Leende tel: 040 2065030/06 2325 06 53 skype adres: frans.marcelissen email: frans.marcelissen at digipsy.nl ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
Take a look at the documentation as to what "%y" means: ?%y? Year without century (00-99). On input, values 00 to 68 are prefixed by 20 and 69 to 99 by 19 - that is the behaviour specified by the 2004 and 2008 POSIX standards, but they do also say ?it is expected that in a future version the default century inferred from a 2-digit year will change?. This is due to the Y2K problem that maybe you never had to deal with. Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Fri, Aug 30, 2013 at 9:12 AM, Frans Marcelissen <frans.marcelissen at digipsy.nl> wrote:> Hi R-friends, > Can anyone explain the following strange behavior to me? >> as.Date( "4/25/71","%m/%d/%y") > [1] "1971-04-25" >> as.Date( "4/25/62","%m/%d/%y") > [1] "2062-04-25" > > so 71 is converted to 1971, while 62 is converted to 2062? Does anyone know > why? And is there a simple way to specify the date? > (does works the same way in R 2.01 as well as in 1.9) > > ------------------- > dr F.H.G. (Frans) Marcelissen > DigiPsy (www.DigiPsy.nl <http://www.digipsy.nl/>) > Pomperschans 26 > 5595 AV Leende > tel: 040 2065030/06 2325 06 53 > skype adres: frans.marcelissen > email: frans.marcelissen at digipsy.nl > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.