3

I'm trying to use as.Date with some data, but I encounter NAs with specific months. I created a data.frame named Fake to test it, and it gave me the same error : it works with September but not with August.

Fake <- c("Sep 12 2014", "Aug 12 2014")
as.Date(Fake, format ="%b %d %Y")

This is what I get:

> as.Date(Fake, format ="%b %d %Y")
[1] "2014-09-12" NA  

I need to use June data also but it doesn't work, even if July data does. Anyone can help?

Naomi Peer
  • 367
  • 2
  • 10
  • 1
    Hmm, works for me. What's your `R.version.string`? Mine is `[1] "R version 3.1.3 (2015-03-09)"`. – bgoldst Apr 01 '15 at 00:29
  • I have this one -> $version.string [1] "R version 3.1.2 (2014-10-31)" How can I get the one you have? – Naomi Peer Apr 01 '15 at 00:32
  • Oh, it might be [locale](https://stat.ethz.ch/R-manual/R-devel/library/base/html/locales.html) related. What's your `Sys.getlocale()`? (Mine is `[1] "en_CA.utf-8/en_CA.utf-8/en_CA.utf-8/C/en_CA.utf-8/en_CA.utf-8"`.) – bgoldst Apr 01 '15 at 00:36
  • @Evenlyne1991, you can upgrade to the latest R version, but since you're already using a quite recent version, that's probably not the issue. – bgoldst Apr 01 '15 at 00:38
  • Also, can you run `strftime(sprintf('2015-%02d-01',1:12),format='%b')` and show me the output? (Mine is `[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"`.) – bgoldst Apr 01 '15 at 00:42
  • fr_CA.UTF-8/fr_CA.UTF-8/fr_CA.UTF-8/C/fr_CA.UTF-8/fr_CA.UTF-8 for the locale. And THAT'S IT -> > strftime(sprintf('2015-%02d-01',1:12),format='%b') [1] "jan" "fév" "mar" "avr" "mai" "jui" "jul" "aoû" "sep" "oct" "nov" "déc" (Obviously this is the problem because I am a French speaker using a French R Studio... but how can I get the English one?) – Naomi Peer Apr 01 '15 at 00:43

2 Answers2

4

The issue is that you have a French locale, which uses different month and day names and abbreviations. You can change to the English locale by running:

Sys.setlocale('LC_ALL','en_CA.utf-8');

Edit: You might also/instead have to run this (I've found this to be necessary in RStudio):

Sys.setlocale('LC_ALL','English');

References that might be useful to people:

Community
  • 1
  • 1
bgoldst
  • 34,190
  • 6
  • 38
  • 64
  • Except... it still doesn't work -_- ' > strftime(sprintf('2015-%02d-01',1:12),format='%b') [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec" > Fake <- c("Sep 12 2014", "Aug 12 2014") > as.Date(Fake, format ="%b %d %Y") [1] "2014-09-12" NA – Naomi Peer Apr 01 '15 at 00:49
  • Hmm, can you run `unname(sapply(c('LC_ALL','LC_COLLATE','LC_CTYPE','LC_MONETARY','LC_NUMERIC','LC_TIME'), Sys.getlocale ))` and show me the output? (Mine is `[1] "en_CA.utf-8/en_CA.utf-8/en_CA.utf-8/C/en_CA.utf-8/en_CA.utf-8" "en_CA.utf-8" "en_CA.utf-8" "en_CA.utf-8" "C" "en_CA.utf-8"`.) – bgoldst Apr 01 '15 at 00:53
  • I can't read the symbol in : LC‌​_TIM after the "LC", it doesn't give anything in my console. – Naomi Peer Apr 01 '15 at 00:55
  • I know! This is very strange. I think there might be something wrong on Stack Overflow's end here. When we copy the text out of the comment, there are [zero-width space](http://www.fileformat.info/info/unicode/char/200B/index.htm) and [zero-width non-joiner](http://www.fileformat.info/info/unicode/char/200C/index.htm) characters embedded after the LC in LC_TIME. I can't seem to get rid of them, no matter how I try to edit the comment. You're going to have to manually type out the remainder of the code, starting from the C in LC_TIME. – bgoldst Apr 01 '15 at 00:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74219/discussion-between-evelyne1991-and-bgoldst). – Naomi Peer Apr 01 '15 at 01:01
1

You should open try opening your terminal and run the following command in the prompt :

defaults write org.R-project.R force.LANG en_US.UTF-8

Then relaunch R or RStudio and try running your code again. Everything should work. If you need further information you can check this blog :

http://mito.air-nifty.com/mitoakiyoshiblog/2010/03/how-to-change-l.html

leakciM
  • 82
  • 2
  • 14