I had a problem that I don’t understand, and I could not find the answer to this problem on this website (I continue to answer questions about how to convert dates to numeric or vice versa, but this is exactly what I do not want know).
The problem is that R converts values that are formatted as a date (for example, "20-09-1992") to numeric values when you assign them to a matrix or data frame. For example, we have "20-09-1992" with the date format, we checked this with class() .
as.Date("20-09-1992", format = "%d-%m-%Y") class(as.Date("20-09-1992", format = "%d-%m-%Y"))
Now we assign this value to the matrix, figuratively called the Matrix:
Matrix <- matrix(NA,1,1) Matrix[1,1] <- as.Date("20-09-1992", format = "%d-%m-%Y") Matrix[1,1] class(Matrix[1,1])
Suddenly, the previous date, formatted "20-09-1992", became a numeric value with a value of 8298. I do not want a numeric value with a value of 8298, I need a date that looks like "20-09-1992", in a date format.
So I was wondering if R just works, and we are not allowed to assign dates for matrices and data frames (somehow I managed to have dates in other matrices / data frames, but it hits me, why those other times were different)? Is there a special method for assigning dates to data frames and matrices that I missed and could not deduce from previous (somehow successful) attempts to assign dates to data frames / matrices?