-1

I have been given a big dataset, where one column is the date in factor format. I want to change this to date format, and uses as.Date

ID <- c(1,2,3,4,5)
dates <- as.factor(c('20OCT2008:00:00:00', '18NOV2008:00:00:00', '05JUN2009:00:00:00', '03MAY2009:00:00:00', '10APR2009:00:00:00'))
df  <- data.frame(ID, dates)

df$dates = as.Date(df$dates, format='%d%b%Y:%H:%M:%S')

df
  ID      dates
1  1       <NA>
2  2 2008-11-18
3  3 2009-06-05
4  4       <NA>
5  5 2009-04-10

However, only some dates are changed, while the rest occurs as NA. Any ideas to how I can transform all dates successfully or why my NAs occure?

Thx!

user163829
  • 23
  • 3
  • It works fine for me. I think it can be locale issue. What is your locale ? – Ronak Shah Jan 12 '20 at 14:07
  • Denmark. So now I've changed the locale to all english, and the code is running. Thx! – user163829 Jan 12 '20 at 14:35
  • If you don't want to change the locale as the would affect the entire sessoin then R does provide a `month.abb` which is always in English. `dates2 <- gsub("([A-Z]+)", ":\\1:", dates); d <- read.table(text = dates2, as.is = TRUE, sep = ":");; with(d, ISOdatetime(V3, match(V2, toupper(month.abb)), V1, V4, V5, V6))` – G. Grothendieck Jan 12 '20 at 15:40

1 Answers1

4

This is likely due to the locale (the language) you are using on your system. Is it English or something else? You can figure this out with:

Sys.getlocale()

If it's not English, you may want to set it to English and run the same command:

Sys.setlocale(locale = "en_US")

In some cases, if you are importing from a spreadsheet, it may be an issue of some white space before or after the date... trimws() can be used to remove white space at the beginning or end of a string.

giocomai
  • 3,043
  • 21
  • 24