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

    file.rename(): Renaming Single and Multiple Files in R

    To rename a file in R, you can use the file.rename() function. It renames a…

    4 hours ago

    R prop.table() Function

    The prop.table() function in R calculates the proportion or relative frequency of values in a…

    10 hours ago

    exp() Function: Calculate Exponential of a Number in R

    The exp() is a built-in function that calculates the exponential of its input, raising Euler's…

    11 hours ago

    R split() Function: Splitting a Data

    The split() function divides the input data into groups based on some criteria, typically specified…

    1 week ago

    colMeans(): Calculating the Mean of Columns in R Data Frame

    The colMeans() function in R calculates the arithmetic mean of columns in a numeric matrix,…

    2 weeks ago

    rowMeans(): Calculating the Mean of rows of a Data Frame in R

    The rowMeans() is a built-in, highly vectorized function in R that computes the arithmetic mean…

    3 weeks ago