[R] strange conversion char to date

arun smartpink111 at yahoo.com
Fri Aug 30 18:29:41 CEST 2013



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.




More information about the R-help mailing list