I have a dataset including a date column in the format of " yyyy-mm-dd", however, when I import it to R, some random numbers come up. How can I solve this issue?
Many thanks.
I have a dataset including a date column in the format of " yyyy-mm-dd", however, when I import it to R, some random numbers come up. How can I solve this issue?
Many thanks.
I'm guessing you are importing from excel. In excel (and every language actually), a date is an integer number corresponding to the number of days since yyyy-mm-dd (the start date varies from program to program). When you are importing those in R, R only sees the integer value and not the formatting used by excel (what excel sees).
To convert those numbers to a date in R, you must use as.Date with the origin 1900-01-01, which is the excel origin (check this by opening excel, writing a '1' in any cell and change the formatting of that cell to Date).
as.Date(1, origin = as.Date("1899-12-31"))
(The change in date is because R adds one day to the origin whereas excel starts at day 1)
It is hard to say since you have given so little information, but I am assuming that you are trying to import an excel spreadsheet into R and the dates are coming through as number of days since Jan 1, 1900 or Jan 1, 1904 depending on whether the file was created on a Windows PC or a Mac. The easiest and safest way to fix this issue would be to open the document in Excel, convert the date format to the yyyy-mm-dd, then save the file as a CSV, then load into R.
Alternatively, you can investigate using the as.Date(yourdatecolumn, origin="1900-01-01")
command where the origin is the date from where your column starts counting days. This gets messy though so I would stick with my first method.