20

I have run into an issue I do not understand, and I have not been able to find an answer to this issue on this website (I keep running into answers about how to convert dates to numeric or vice versa, but that is exactly what I do not want to know).

The issue is that R converts values that are formatted as a date (for instance "20-09-1992") to numeric values when you assign them to a matrix or data frame. For example, we have "20-09-1992" with a date format, we have checked this using class().

as.Date("20-09-1992", format = "%d-%m-%Y")
class(as.Date("20-09-1992", format = "%d-%m-%Y"))

We now assign this value to a matrix, imaginatively called 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 previously date formatted "20-09-1992" has become a numeric with the value 8298. I don't want a numeric with the value 8298, I want a date that looks like "20-09-1992" in date format.

So I was wondering whether this is simply how R works, and we are not allowed to assign dates to matrices and data frames (somehow I have managed to have dates in other matrices/data frames, but it beats me why those other times were different)? Is there a special method to assigning dates to data frames and matrices that I have missed and have failed to deduce from previous (somehow successful) attempts at assigning dates to data frames/matrices?

Sotos
  • 44,023
  • 5
  • 28
  • 55
Joes de Natris
  • 303
  • 1
  • 2
  • 5
  • 1
    A `matrix` in R stripes all attributes from the input vector. This documented in `?matrix`: "*an optional data vector (including a list or expression vector). Non-atomic classed R objects are coerced by as.vector and all attributes discarded.*". If you''ll run `attributes(as.Date("20-09-1992", format = "%d-%m-%Y"))` you''ll see that `class` is an attribute. When the class is stripped, the `Date` vector is just goes back to it's storage mode which is `storage.mode(as.Date("20-09-1992", format = "%d-%m-%Y"))`. Also see `unclass(as.Date("20-09-1992", format = "%d-%m-%Y"))` – David Arenburg Jan 17 '17 at 14:50
  • Thanks! Also for the other comment! – Joes de Natris Jan 17 '17 at 17:19

2 Answers2

7

I don't think you can store dates in a matrix. Use a data frame or data table. If you must store dates in a matrix, you can use a matrix of lists.

Matrix <- matrix(NA,1,1)
Matrix[1,1] <- as.list(as.Date("20-09-1992", format = "%d-%m-%Y"),1)
Matrix
[[1]]
[1] "1992-09-20"

Edited: I also just re-read you had this issue with data frame. I'm not sure why.

mydate<-as.Date("20-09-1992", format = "%d-%m-%Y")
mydf<-data.frame(mydate)
mydf

      mydate
1 1992-09-20

Edited: This has been a learning experience for me with R and dates. Apparently the date you supplied was converted to number of days since origin. Origin is defined as Jan 1st,1970. To convert this back to a date format at some point

Matrix
     [,1]
[1,] 8298

as.Date(Matrix, origin ="1970-01-01")
[1] "1992-09-20" 
akaDrHouse
  • 1,781
  • 2
  • 15
  • 22
  • 1
    Thank you very much! I found a work around (storing the date as a character, and then later changing the format to as.date when calling the character in a loop or function), lubridate sounds just like what I needed to do this more simply! So, again, thanks! – Joes de Natris Jan 17 '17 at 14:32
5

try the following: First specify your date vector & then use

rownames(mat) <- as.character(date_vector)

the dates will appear as a text.

MAhipal Singh
  • 4,220
  • 35
  • 53
Marco
  • 51
  • 1
  • 1