R as.Date() Function: Working with Dates

The as.Date() function in R converts various types of date and time objects or character strings into Date objects.

Syntax

as.Date(x, format, tryFormats = c("%Y-%m-%d", "%Y/%m/%d"),
        optional = FALSE, tz = "UTC")

Parameters

Name Description
x It is an object to be converted.
format It is a character string. If the format argument is not specified, it will tryFormats one by one on the first non-NA element and give an error if none works. Otherwise, the processing is via strptime.
tryFormats It is a character vector of format strings to try if the format is not specified.
optional It is a logical argument to return NA (instead of signaling an error) if the format guessing fails.
origin It is a Date object or something that can be coerced.Date(origin, …) to such an object.
tz It is a time zone name.

    Return Value

    It returns the object of class “Date“.

    Example 1: Basic usage

    How to use as.Date() function

    In the above figure, we converted character vectors into the vector of dates in %d%b%Y format.

    dates <- c("20nov1980", "20nov1991", "20nov1993", "10sep1993")
    
    dt <- as.Date(dates, "%d%b%Y")
    
    dt

    Output

    [1] "1980-11-20" "1991-11-20" "1993-11-20" "1993-09-10"

    Example 2: Converting numeric values to dates

    Numeric values can also be converted to dates. By default, numeric conversion assumes the number represents the number of days since 1970-01-01 (the Unix epoch):

    # Numeric representation of a date (days since 1970-01-01)
    date_numeric <- 19320
    
    # Convert to Date
    date_object <- as.Date(date_numeric, origin = "1970-01-01")
    
    # Print the result
    print(date_object)
    

    Output

    [1]  "2022-11-24"
    

    Example 3: Unrecognizable Date Formats

    Use as.Date() with Unrecognizable Date Formats

    In the above figure, we took a vector of unrecognizable date formats and converted it into a vector of proper dates.

    dates <- c("11/20/80", "11/20/91", "11/20/1993", "09/10/93")
    
    dt <- as.Date(dates, "%m/%d/%y")
    
    dt

    Output

    [1] "1980-11-20" "1991-11-20" "2019-11-20" "1993-09-10"

    Example 4: Setting timezone

    To set the timezone, use the tz argument.

    If your computer system’s time is set to PST8PDT, you can use the tz=” “ argument, which means “use the current system time zone”.

    Using as.Date() function with timezone parameter

    In the above figure, we passed the timezone explicitly using the “tz” argument that the output is a proper date.

    as.Date("2022/05/27 12:30", tz = "PST8PDT")

    Output

    [1] "2022-05-27"

    Example 4: Returning NA argument

    The as.date() returns NA if you have not set the locale properly per your system.

    Example of as.date() function returns NA

    The above figure describes that if you don’t set the locale properly as per your system, it will return the “NA” value.

    factr <- as.factor("27/05/2022")
    date <- as.Date(factr, format = "%Y-%m-%d")
    
    print(factr)
    print(date)
    

    Output

    [1] 27/05/2022
    Levels: 27/05/2022
    
    [1] NA

    To handle NA values, convert the factor into POSIXt and a date.

    Convert the factor into POSIXt and then into a date

    a <- as.factor("27/05/2022")
    pos_dt <- strptime(a, format = "%d/%m/%Y")
    b <- as.Date(pos_dt, format = "%Y-%m-%d")
    print(a)
    print(b)

    Output

    [1] 27/05/2022
    Levels: 27/05/2022
    [1] "2022-05-27"

    That’s it.

    Leave a Comment