1

I cannot get R to format my column as a date, no matter what I do. I have even gone so far as to create a new string of dates separately and use cbind to add these to the dataframe I want them on, but they are still not recognized as a date. R lists the column as "unknown" when I hover over it. I'm not sure what is going on here.

values = seq(from = as.Date("2016-04-8"), to = as.Date("2017-05-30"), by = 'day')
AllChlData<-cbind(AllChlData, NewDate =values)
AllChlData$NewDate <- as.Date(AllChlData$NewDate, "%m/%d/%Y")

Everything combines into my dataframe just fine, everything looks correct in there, but it is just not recognizing as a date no matter what I try. Any ideas?

Steve G
  • 85
  • 9
  • 1
    This: `as.Date(AllChlData$NewDate, "%m/%d/%Y")` doesn't "re-format" a date column so that it prints differently. It thinks you're trying to convert something that is *already* in m/d/Y format into a date column. You can change the display format (and make it a character column) using `format`. – joran Jun 04 '19 at 14:53
  • I poorly worded this I guess. I just want R to recognize the column as dates, and it won't do that. It says the column is "unknown", not dates, not numeric, not a character; just "unknown" – Steve G Jun 04 '19 at 15:00
  • 1
    Joran is correct. The line `AllChlData$NewDate <- as.Date(AllChlData$NewDate, "%m/%d/%Y")` is unnecessary as you already created a date vector in `seq(…)`. Still, I ran your code with my own data and everything works just fine. Instead of "hovering" over the column, have you tried calling `class(AllChlData$NewDate)`? Sometimes the IDEs have bugs, but the data might be just fine. Is `AllChlData` maybe a matrix and not a dataframe? R would coerce the dates in that case. –  Jun 04 '19 at 15:00
  • 1
    So it recognizes it as a date for me in the tests I've done. What does it say about the column when you run `str()` on the data frame? I would also echo the concerns about AllChlData being a matrix; more broadly, adding data frame columns using `cbind` is dangerous and you should avoid it. – joran Jun 04 '19 at 15:02
  • '> class(AllChlData$NewDate) [1] "Date"' ' str(AllChlData)' lists it as a date as well. It's strange that when I hover over it, it's not listed as a date. I didn't originally think it was working at all because when I try to convert my original date in the data frame to a date, it was displaying as unknown as well- which is why I went to creating a whole new column. Thank you all. – Steve G Jun 04 '19 at 15:18

0 Answers0