Learn R Programming

Tivy (version 0.1.1)

convert_to_date: Convert dates to standard format

Description

Converts a vector of dates in various formats to a standard date format. The function tries to parse each date using predefined formats and returns the first valid date found for each entry. If a date cannot be interpreted, it is assigned as NA.

Usage

convert_to_date(date_vector, output_type = c("date", "datetime"))

Value

A vector of Date or POSIXct objects, or NA if the date cannot be converted.

Arguments

date_vector

A character vector containing dates in various formats.

output_type

Type of object to return: "date" for Date, "datetime" for POSIXct.

Examples

Run this code
dates <- c("2025-04-10", "10/04/2025", "April 10, 2025")
converted_dates <- convert_to_date(dates)
print(converted_dates)

Run the code above in your browser using DataLab