0

I'm using as.Date for a dataset I have with dates as a variable that I'm using on the x axis of a graph. I had everything fine and the graph output was great, but something happened and now there are missing dates and formatting with things like %Y and %d aren't working!

Can someone please help me figure out how to get the data to feed in correctly?


IHS <- read.csv("IHS 20-21.csv")
IHS$Start.Date <- as.Date(IHS$Start.Date)

This is the code I'm using. If you need more info I can share more, not sure what bits of code would be helpful here, but these two lines are where the problem is. If I feed in the data and check the environment the table looks fine. But then when I add in the as.Date it starts to lose dates.

The dates are in an excel column as follows: excel dates

And I'm trying to get the dates in YYYY/MM/DD format, but I'll be happy with any format as long as the data is all there!

Stefani
  • 19
  • 1
  • 1
    `as.Date(IHS$Start.Date, format = "%m/%d/%Y")` _might_ work. But it might not depending on if what is seen in your image is the raw value or if it is a mask on top of something else. We could answer with more certainty if you could show us the result of `dput(head(IHS$Start.Date))`. – Benjamin Jun 14 '21 at 19:45
  • 1
    Thank you, when I used the code dput(head(IHS$Start.Date)) this is the output: structure(c(-715917, NA, NA, -715825, -715613, NA), class = "Date") – Stefani Jun 14 '21 at 20:11
  • This thread might help https://stackoverflow.com/a/43230524/10276092 – M.Viking Jun 14 '21 at 20:18
  • @M.Viking Thank you, I'm a bit lost of what the thread is talking about as an origin? Would that be the first date in the excel sheet column? – Stefani Jun 14 '21 at 20:26

1 Answers1

1

Based on the comments where the structure is listed as c(-715917, NA, NA, -715825, -715613, NA), it looks to me like your dates came in as unix epoch time referenced to origin 1-1-1970. You can use lubridate to convert.

Example:

library(lubridate)
lubridate::as_date(c(-715917, NA, NA, -715825, -715613, NA))

[1] "9-11-20"  NA         NA         "10-02-20" "10-09-20" NA  

To structure YYYY-MM-DD use ymd():

library(lubridate)
IHS$Start.Date <- ymd(as_date(c(-715917, NA, NA, -715825, -715613, NA)))

https://cran.r-project.org/web/packages/lubridate/index.html

Neal Barsch
  • 2,810
  • 2
  • 13
  • 39