R Basic

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",
        origin,
        ...)

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.

    Basic usage

    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"

    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"
    

    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"

    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”.

    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"

    Returning NA argument

    The as.date() function returns NA if the locale has not been set properly according to your system.

    The above figure illustrates that if you don’t set the locale properly according to 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.

    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.

    Recent Posts

    R View() Function

    The View() is a utility function in R that invokes a more intuitive spreadsheet-style data…

    5 days ago

    summary() Function: Producing Summary Statistics in R

    The summary() is a generic function that produces the summary statistics for various R objects,…

    2 weeks ago

    R paste() Function

    The paste() function in R concatenates vectors after converting them to character. paste("Hello", 19, 21,…

    3 weeks ago

    paste0() Function in R

    R paste0() function concatenates strings without any separator between them. It is a shorthand version…

    3 weeks ago

    How to Calculate Standard Error in R

    Standard Error (SE) measures the variability or dispersion of the sample mean estimate of a…

    3 weeks ago

    R max() and min() Functions

    max() The max() function in R finds the maximum value of a vector or data…

    4 weeks ago