How do you change numerical values in R into dates?

Viewed 303

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
1 Answers

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"
    
Related