[R] question about formatting Dates

Patrick Connolly p_connolly at slingshot.co.nz
Wed Jun 27 08:50:54 CEST 2012


On Tue, 26-Jun-2012 at 10:54PM -0500, Erin Hodgess wrote:

|> Dear R People:
|> 
|> I have dates as factors in the following:
|> 
|> > poudel.df$DATE
|> [1] 1/2/2011  1/4/2011  1/4/2011  1/4/2011  1/6/2011  1/7/2011  1/8/2011
|> [8] 1/9/2011  1/10/2011
|> Levels: 1/10/2011 1/2/2011 1/4/2011 1/6/2011 1/7/2011 1/8/2011 1/9/2011
|> >
|> 
|> I want them to be "regular" dates which can be sorted, etc.
|> 
|> But when I did this:
|> 
|> > as.character(poudel.df$DATE)
|> [1] "1/2/2011"  "1/4/2011"  "1/4/2011"  "1/4/2011"  "1/6/2011"  "1/7/2011"
|> [7] "1/8/2011"  "1/9/2011"  "1/10/2011"
|> 
|> and
|> > as.Date(as.character(poudel.df$DATE),"%m/%d/$Y")
|> [1] NA NA NA NA NA NA NA NA NA
|> 
|> because the dates do not have leading zeros.

I don't think that's the reason why.

> as.Date(c("1/2/2011",  "1/4/2011",  "1/4/2011"), format = "%m/%d/%Y")
[1] "2011-01-02" "2011-01-04" "2011-01-04"
> 

Leading zeros aren't essential.  




|> 
|> There are approximately 30 years of nearly daily data in the entire set.
|> 
|> Any suggestions would be much appreciated.
|> 
|> Sincerely,
|> Erin
|> 
|> 
|> -- 
|> Erin Hodgess
|> Associate Professor
|> Department of Computer and Mathematical Sciences
|> University of Houston - Downtown
|> mailto: erinm.hodgess at gmail.com
|> 
|> ______________________________________________
|> 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.

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___    Patrick Connolly   
 {~._.~}                   Great minds discuss ideas    
 _( Y )_  	         Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)  	                      ..... Eleanor Roosevelt
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.



More information about the R-help mailing list