-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
  • 11
  • 2

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
  • 2,385
  • 14
  • 24