5

I am running a code my colleague has written on his Rstudio some time ago and I am stuck on what appears to be a very silly problem. I have a variable in a dataframe that is a character and represents a date, such as

30jan2016
38feb2016

I need to transform this character string into a date variable and I am using the following:

df$newvar <- format(as.Date(df$oldvar, format="%d%b%Y"), format="%Y%m%d")

This is what I get:

NA
2016-02-28

After some though I realised that my Rstudio is reading the "month" part of the string in Italian (below you can find the comparison of month abbreviations ENG-ITA), and is correctly decoding only what it can read in ITALIAN. Changing global language options didn't help (and anyway R language was already set to English). Any ideas?

Thanks!

ENG    ITA
jan    gen
feb    feb
mar    mar
apr    apr
may    mag
jun    giu
jul    lug
aug    ago
sep    set
oct    ott
nov    nov
dec    dic

NOTE: Of course I could replace parts of the string and then run the piece of code, but I need this code to run on ANY computer, in English.

Erica
  • 51
  • 1
  • 4
  • 3
    What day is `38feb2016` ? – jogo Jan 31 '17 at 14:55
  • 3
    What's the output of `Sys.getlocale()`? You probably need to change your locale with `Sys.setlocale()`. E.g: `Sys.setlocale("LC_TIME", "en_GB.UTF-8")` – Jaap Jan 31 '17 at 15:01
  • @Jaap that did the job, thanks. If you post your comment as an answer I can mark it as answered. – Erica Jan 31 '17 at 15:25

1 Answers1

3

Try readr. There is a possibility to manually set the locale.

library(readr)
parse_date(c("30jan2016", "1feb2017"), "%d%b%Y", locale = locale("en"))
Iaroslav Domin
  • 2,698
  • 10
  • 19