2

Hi this question has been bugging me for some time.

So I am trying to convert the so-called dates in my R project into actual dates. Right now the dates are arranged in a numerical manner, ie after 2/28/2020 it's not 3/1/2020 but 2/3/2020.

I've tried the

as.Date(3/14/2020, origin = "14-03-2020")

and also

df <- data.frame(Date = c("10/9/2009 0:00:00", "10/15/2009 0:00:00"))
 as.Date(df$Date, "%m/%d/%Y %H:%M:%S")

and

 strDates <- c("01/28/2020", "05/03/2020")%>%
  dates <- as.Date(strDates, "%m/%d/%Y")

i just plugged in two dates to test out if it works or not because there are about around 40 dates. However, my output is as follows:

Error in as.Date.default(., 3/14/2020, origin = "14-03-2020") : do not know how to convert '.' to class “Date” 

for the first one and then

the second one is:

data frame not found 

the third one is

Error in as.Date(strDates, "%m/%d/%Y") : object 'strDates' not found
Henry Cyranka
  • 2,550
  • 1
  • 10
  • 19
edwina lai
  • 29
  • 1
  • 3
    Some of that is not legal or realistic code. For instance, in `as.Date(3/14/2020)`, that's replacing your `3/14/2020/` with `0.000106082`, because that's what 3 divided by 14 divided by 2020 resolves to. You need to identify it as a string with single- or double-quotes, as in `as.Date("3/14/2020")`. The second problem there is that your origin needs to be unambiguous to R, so `origin="2020-03-14"`, as shown in the examples in [`?as.Date`](https://stat.ethz.ch/R-manual/R-patched/library/base/html/as.Date.html). – r2evans May 13 '20 at 22:02
  • 2
    The error about converting "." to a date is because you have `c(...) %>% dates %` is injecting the `.` as the first argument of the next command, effectively making it `as.Date(., strDates, "%m/%d'%Y")`, which is obviously not what you intended. Remove the `%>%` and try that part again. – r2evans May 13 '20 at 22:05
  • 1
    Lastly, you have some really confused standards on format here. You start using `origin="14-03-2020"` which suggests `"%d-%m-%Y"`, but then you later reference `"%m/%d/%Y"` (which appears to be more appropriate given some of the strings you have provided). – r2evans May 13 '20 at 22:07
  • 1
    Post-lastly ... you use `origin=` when you provide a numeric date. Use `format=` when you provide a string date. The two generally do not cross. – r2evans May 13 '20 at 22:08
  • 1
    `as.Date("3/14/2020", format = "%m/%d/%Y")` and `as.Date(c("01/28/2020", "05/03/2020"), format = "%m/%d/%Y")` – r2evans May 13 '20 at 22:09
  • 1
    @r2evans Combine your comments to one answer. It's quite useful for learning the use of `as.Date` and formatting dates. :-) – Martin Gal May 13 '20 at 22:11

1 Answers1

1

Issues with your code:

  1. as.Date(3/14/2020, origin = "14-03-2020")

    First, R will replace 3/14/2020 with 0.000106082, since that's what 3 divided by 14 divided by 2020 equals. You need to identify it as a string using single or double quotes, as in: as.Date("3/14/2020", origin = "14-03-2020").

  2. But that is still broken. When converting to Date, if you provide a character (string) input, then you may need to provide format=, since it needs to know which numbers in the string correspond to year, month, date, etc. If you provide a numeric (or integer) input, then you do need to provide origin=, so that it knows what "day 0" is. For unix, epoch is what you need, so origin="1970-01-01". If you're using dates from Excel, you need origin="1899-12-30" (see https://stackoverflow.com/a/43230524).

  3. Your next error is because you are mixing magrittr ops with ... base R.

     strDates <- c("01/28/2020", "05/03/2020")%>%
      dates <- as.Date(strDates, "%m/%d/%Y")
    

    The issue here has nothing to do with dates. The use of %>% on line 1 is taking the output of line 1 (in R, assignment to a variable invisibly returns the assigned numbers, which is why chaining assignment works, a <- b <- 2) and injecting it as the first argument in the next function call. With this your code was eventually interpreted as

     strDates <- c("01/28/2020", "05/03/2020")%>%
       { dates <- as.Date(., strDates, "%m/%d/%Y") }
    

    which is obviously not what you intended or need. I suspect that this is just an artifact of getting frustrated and was mid-stage converting from a %>% pipe to something else, and you forgot to clean up the %>%s. This could be

    dates <- c("01/28/2020", "05/03/2020") %>%
      as.Date("%m/%d/%Y")
    dates
    # [1] "2020-01-28" "2020-05-03"
    
  4. Your data.frame code seems to work fine, though you do not assign the new Date-assigned values back to the frame. Try this slight adaptation:

    df <- data.frame(Date = c("10/9/2009 0:00:00", "10/15/2009 0:00:00"))
    df$Date <- as.Date(df$Date, "%m/%d/%Y %H:%M:%S")
    df
    #         Date
    # 1 2009-10-09
    # 2 2009-10-15
    str(df)
    # 'data.frame': 2 obs. of  1 variable:
    #  $ Date: Date, format: "2009-10-09" "2009-10-15"
    
r2evans
  • 77,184
  • 4
  • 55
  • 96
  • thank you so much for the answer. 1. oh I see I am going to change it to 4 now. I just tried and it works perfectly for 4 but there is only a minor issue left: it shows the list of date in the correct format in another chart data_frame. I just want to ask how I can incorporate it to my actual data set to create a graph. Thank you very much for the update, I have just used it for a week or so, so this helps a lot and I have learnt so much. – edwina lai May 14 '20 at 06:00
  • I have no idea what you're talking about. `data_frame`? graph? None of that was included in the question. `Date` objects can be plotted just like any `numeric` or `integer`. – r2evans May 14 '20 at 14:34
  • I am sorry for not giving enough information. I mean I have a certain data set containing the messy dates and now that I have created a data frame for dates, how do I incorporate it into the original data set? Merge(RKI_COVID19_200504, date = dt?) the RKI_COVID19_200504 is the file name. Thanks again. Really helps a lot. – edwina lai May 15 '20 at 15:25