[R] Problem with DateVisit-gives wrong year?

Pancho Mulongeni p.mulongeni at namibia.pharmaccess.org
Wed Apr 17 15:18:59 CEST 2013


Thank you,
I see this was due to me using %y instead of %Y,
See ?strptime

-----Original Message-----
From: arun [mailto:smartpink111 at yahoo.com] 
Sent: 17 April 2013 12:31
To: Pancho Mulongeni
Cc: R help
Subject: Re: [R] Problem with DateVisit-gives wrong year?

HI,
test.date<- rep(c("14/05/2012","01/10/2012","28/09/2012"),each=6)
 as.Date(test.date,"%d/%m/%Y")
# [1] "2012-05-14" "2012-05-14" "2012-05-14" "2012-05-14" "2012-05-14"
 #[6] "2012-05-14" "2012-10-01" "2012-10-01" "2012-10-01" "2012-10-01"
#[11] "2012-10-01" "2012-10-01" "2012-09-28" "2012-09-28" "2012-09-28"
#[16] "2012-09-28" "2012-09-28" "2012-09-28"
 test.date1<- factor(test.date)
 as.Date(test.date1,"%d/%m/%Y")
# [1] "2012-05-14" "2012-05-14" "2012-05-14" "2012-05-14" "2012-05-14"
 #[6] "2012-05-14" "2012-10-01" "2012-10-01" "2012-10-01" "2012-10-01"
#[11] "2012-10-01" "2012-10-01" "2012-09-28" "2012-09-28" "2012-09-28"
#[16] "2012-09-28" "2012-09-28" "2012-09-28"
A.K.




----- Original Message -----
From: Pancho Mulongeni <p.mulongeni at namibia.pharmaccess.org>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Wednesday, April 17, 2013 7:18 AM
Subject: [R] Problem with DateVisit-gives wrong year?

Hi I have the following factor of dates that I want to converted to Date class so I can extract the month
> test.date
[1] 14/05/2012 14/05/2012 14/05/2012 14/05/2012 14/05/2012 14/05/2012 [7] 14/05/2012 14/05/2012 14/05/2012 14/05/2012
201 Levels: 01/10/2012 01/11/2012 01/12/2012 02/07/2012 ... 28/09/2012 I use code below
ntest.date<-as.Date(test.date,'%d/%m/%y')

but the output has the wrong year, and the reverse order ntest.date [1] "2020-05-14" "2020-05-14" "2020-05-14" "2020-05-14" "2020-05-14"
[6] "2020-05-14" "2020-05-14" "2020-05-14" "2020-05-14" "2020-05-14"

What am I doing wrong?
I dare not say the word 'bug'
Thanks
Pancho Mulongeni
Research Assistant
PharmAccess Foundation
1 Fouché Street
Windhoek West
Windhoek
Namibia
 
Tel:   +264 61 419 000
Fax:  +264 61 419 001/2
Mob: +264 81 4456 286

______________________________________________
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