[R] Weird date conversion

peter dalgaard pdalgd at gmail.com
Wed May 23 14:35:59 CEST 2012


On May 23, 2012, at 11:23 , Kony-2012 wrote:

> I am a new R user, but have searched the manuals on the topic , but found no
> suitable solution.
> I have imported a 20 years’ time-series data from csv format into R.  The
> date variable was a factor and I used the command “  x$date <-
> as.Date(date,format= "%d%b%Y") “ to convert to a date format. The conversion
> worked for most dates, but all data that corresponds to the months of May
> and October for all years returned NAs. Strangely, I had experienced similar
> problem with a different data set on another computer using earlier version
> of R  some time ago.  The computer that I use now is a brand new
> installation of R and other software.
> 
> I am running R version 2.14.2 on Windows 7. 
> I would appreciate if someone would help on this recurring problem.
> 

You really should show us some examples of dates that are incorrectly handled. 

However, a good guess is that it is a locale issue. E.g., Scandinavian languages have "maj" and "oktober" for those two months, so the three letter abbreviations are jan, feb, mar, apr, maj, jun, jul, aug, sep, okt, nov, dec. This obviously matches English locales except exactly in May and October.  The way out would seem to be to use Sys.setlocale to set LC_TIME to match the input before trying as.Date(). (Details are OS dependent, but take a look at the examples in ?Sys.setlocale.)
  



> Thanks, 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Weird-date-conversion-tp4631058.html
> Sent from the R help mailing list archive at Nabble.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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list