1

I have a .xlsx file that I read onto R. This file has one of the columns in date format (d/m/y) but for some reason it's displaying as series of numbers in the data frame on RStudio.

My question is how do I change the column to the original date format? Here's an example of the date that's showing: 887587200 - instead of something like 12/03/1974.

Any help to fix this would be appreciated. Thanks

Helen Neely
  • 4,666
  • 8
  • 40
  • 64

1 Answers1

2

Looks like your dates are being stored as a numeric value, likely the number of seconds since Jan 1, 1970. So to convert the column, you could do:

df$my_col <- as.Date(df$my_col / 60 / 60 / 24, origin = '1970-01-01')

This converts 887587200 to a date of 1998-02-16.

jdobres
  • 11,339
  • 1
  • 17
  • 37
  • Thanks a lot this works. But how do I make the date to be this format dd/mm/yyyy? – Helen Neely Aug 12 '16 at 11:46
  • 1
    @HelenNeely; it reall is in that format already, it just displays differently. You can change how it looks / prints on your screen using `format` (ie `format(x, "%d/%m/%Y")` ), however, this is less useful as it converts the date format to character. – user20650 Aug 12 '16 at 11:59