Helen Neely Helen Neely - 3 months ago 8
R Question

Convert series of numbers to date in R

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

Answer

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.