2

I try to read the date like 03-Nov-11, and I use the code as followed:

s<-c("03-Nov-13")
s1<-as.Date(s,"%d-%b-%y")

but s1 is NA.

Ethaan
  • 11,291
  • 5
  • 35
  • 45
Cheng
  • 193
  • 3
  • 10
  • I've tried this code and it works for me `> s<-c("03-Nov-13")` `> s1<-as.Date(s,"%d-%b-%y")` `> s1` `[1] "2013-11-03"` (On Windows 7, R version 3.1.2) – tospig Apr 04 '15 at 04:10

1 Answers1

3

The %b seems to be problematic indeed. I've had luck with those though:

s1 <- "03-11-13"

as.POSIXct(s1, format = "%d-%m-%y")
# [1] "2013-11-03 EDT"

s2 <- "03-NOVEMBRE-13" # Note that I have Fr locale, hence the ending in "BRE"

as.POSIXct(s2, format = "%d-%B-%y")
# [1] "2013-11-03 EDT"

as.POSIXct(s2, format = "%d-%b-%y")
# [1] "2013-11-03 EDT"

However, the abbreviated version doesn't seem to work at all on Windows:

s3 <- "03-NOV-13"
as.POSIXct(s3, format = "%d-%B-%y")
# [1] NA

as.POSIXct(s3, format = "%d-%b-%y")
# [1] NA

EDIT

After trying on Linux, the %b does what is expected!

as.POSIXct(s3, format="%d-%b-%y")
# [1] "2013-11-03 PDT"

as.POSIXct(s3, format="%d-%B-%y")
# [1] "2013-11-03 PDT"

EDIT 2

Filed a bug report; See https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16301

EDIT 3

On my end I was mistaken -- my locale's months abbreviations are given by:

z <- seq.Date(as.Date('2015-01-01'), by='month', len = 12)
format(z, "%d-%b-%y")

#  [1] "01-janv.-15" "01-févr.-15" "01-mars-15"  "01-avr.-15"  "01-mai-15"
#  [6] "01-juin-15"  "01-juil.-15" "01-août-15"  "01-sept.-15" "01-oct.-15"
# [11] "01-nov.-15"  "01-déc.-15"

So using "nov.", "NOV.", "NOVEMBRE" or "novembre" works fine.

Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61
  • Thank you for your answer! Why the abbreviated version doesn't work in Rstudio? How can I deal with this problem? Because I try to do data mining , I nedd to solve this problem. – Cheng Apr 04 '15 at 03:48
  • It's not solely in RStudio, same behavior in RGUI. I haven't tried on Linux yet, but let me try that now... – Dominic Comtois Apr 04 '15 at 03:52
  • Np. Btw I checked with %h too, and still no luck on Windows (but Linux, fine). – Dominic Comtois Apr 04 '15 at 04:04
  • Can you tell me what hardware and OS you're using? I'm currently filing a bug report. – Dominic Comtois Apr 04 '15 at 04:13
  • Thank you!! I just went to have a lunch, sorry to answer you so late. I just use Rstudio, and the version of R is 3.1.3 x64, the OS is windows 8.1 – Cheng Apr 04 '15 at 04:49
  • I just wonder " Is there any way that I change the locale", because I don't want to change the original data format like "13-Nov-13". Thank you!!! – Cheng Apr 06 '15 at 08:08
  • Maybe see http://stackoverflow.com/questions/8145886/change-time-locale-for-r or http://stackoverflow.com/questions/16347731/how-to-change-the-locale-of-r-in-rstudio – Dominic Comtois Apr 06 '15 at 08:10
  • For me `Sys.setlocale("LC_TIME", "English")` works well. – Dominic Comtois Apr 06 '15 at 08:17
  • All righty! Pls consider accepting the answer (green checkmark), it's good for your rep too! – Dominic Comtois Apr 06 '15 at 08:39