Date conversion problem

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Date conversion problem

natalie.vanzuydam
Hi All,

I have a character data.frame that contains character columns and date columns.  I've manage to convert some of my character columns to a date format using as.Date(x, format="%m/%d/%y").  

An example of one of my dates is
 PROCHI    DtDeath icdcucd date_admission1 date_admission_2
CAO0004563       <NA>    <NA>      2005-09-01             <NA>
CAO0073505       <NA>    <NA>      1998-03-05             <NA>
CAO0079987       <NA>    <NA>      2002-04-14             <NA>
CAO0182089       <NA>    <NA>      2007-06-10         11/06/07
CAO0194809 17/02/2005     I64      2004-09-04         14/02/05
CAO0204000       <NA>    <NA>      1999-05-31             <NA>
CAO0279999       <NA>    <NA>      1999-07-29             <NA>
CAO0330844 29/11/2001     I64            <NA>             <NA>
CAO0395045       <NA>    <NA>      2007-02-13         14/02/07
CAO0507333       <NA>    <NA>      2005-10-08             <NA>


I have converted date_admission1 from a character to a date.  I used the same script to convert DtDeath but it returns the dates in this format:

 NA           NA           NA           NA           "0017-02-20"
 [6] NA           NA           "0029-11-20" NA           NA          
[11] NA           NA           "0013-10-20" NA           NA          
[16] NA           "0007-12-20" NA           NA           NA          
[21] NA           NA           NA           NA           NA          
[26] NA           NA           NA           NA           NA          
[31] NA           NA           NA           NA           NA          
[36] NA           NA           NA           NA           NA          
[41] NA           NA           NA           NA           NA          
[46] NA           "0029-01-20" "0018-05-20" NA           NA          
[51] NA           NA           NA           NA           NA          
[56] NA           "0013-07-20" NA           NA           NA          
[61] NA           "0026-07-20" NA           NA           NA          
[66] "0029-04-20" NA           NA           NA           "0012-12-20"
[71] NA           NA           NA           NA           NA          
[76] NA           NA           NA           NA           NA          
[81] NA           NA           "0022-01-20" NA           "0029-05-20"
[86] NA           NA           NA           NA           "0022-02-20"
[91] NA          

I've tried as.Date(as.character(DtDeath, "%d/%m/%y") just in case and have used different versions of the format (%m/%d/%Y, and the reverse)but still get the incorrect format.  I'm not sure what the problem is?

Thanks,
natalie
Natalie Van Zuydam

PhD Student
University of Dundee
nvanzuydam@dundee.ac.uk
Reply | Threaded
Open this post in threaded view
|

Re: Date conversion problem

Don MacQueen

>  as.Date('17/02/2005','%d/%m/%Y')
[1] "2005-02-17"

(Read the documentation more carefully to distinguish between %y and
%Y;  I guess you tried lots of combinations but never tried the
correct one, so just be more careful at matching what your data is
with the format string you create.)

-Don

At 8:09 AM -0800 3/4/10, Newbie19_02 wrote:

>Hi All,
>
>I have a character data.frame that contains character columns and date
>columns.  I've manage to convert some of my character columns to a date
>format using as.Date(x, format="%m/%d/%y").
>
>An example of one of my dates is
>  PROCHI    DtDeath icdcucd date_admission1 date_admission_2
>CAO0004563       <NA>    <NA>      2005-09-01             <NA>
>CAO0073505       <NA>    <NA>      1998-03-05             <NA>
>CAO0079987       <NA>    <NA>      2002-04-14             <NA>
>CAO0182089       <NA>    <NA>      2007-06-10         11/06/07
>CAO0194809 17/02/2005     I64      2004-09-04         14/02/05
>CAO0204000       <NA>    <NA>      1999-05-31             <NA>
>CAO0279999       <NA>    <NA>      1999-07-29             <NA>
>CAO0330844 29/11/2001     I64            <NA>             <NA>
>CAO0395045       <NA>    <NA>      2007-02-13         14/02/07
>CAO0507333       <NA>    <NA>      2005-10-08             <NA>
>
>
>I have converted date_admission1 from a character to a date.  I used the
>same script to convert DtDeath but it returns the dates in this format:
>
>  NA           NA           NA           NA           "0017-02-20"
>  [6] NA           NA           "0029-11-20" NA           NA        
>[11] NA           NA           "0013-10-20" NA           NA        
>[16] NA           "0007-12-20" NA           NA           NA        
>[21] NA           NA           NA           NA           NA        
>[26] NA           NA           NA           NA           NA        
>[31] NA           NA           NA           NA           NA        
>[36] NA           NA           NA           NA           NA        
>[41] NA           NA           NA           NA           NA        
>[46] NA           "0029-01-20" "0018-05-20" NA           NA        
>[51] NA           NA           NA           NA           NA        
>[56] NA           "0013-07-20" NA           NA           NA        
>[61] NA           "0026-07-20" NA           NA           NA        
>[66] "0029-04-20" NA           NA           NA           "0012-12-20"
>[71] NA           NA           NA           NA           NA        
>[76] NA           NA           NA           NA           NA        
>[81] NA           NA           "0022-01-20" NA           "0029-05-20"
>[86] NA           NA           NA           NA           "0022-02-20"
>[91] NA        
>
>I've tried as.Date(as.character(DtDeath, "%d/%m/%y") just in case and have
>used different versions of the format (%m/%d/%Y, and the reverse)but still
>get the incorrect format.  I'm not sure what the problem is?
>
>Thanks,
>natalie
>--
>View this message in context:
>http://*n4.nabble.com/Date-conversion-problem-tp1578296p1578296.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] 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.


--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062

______________________________________________
[hidden email] 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.