0

I have a dataset with a date column represented in a numeric format, such as "189901" for January 1899, "189902" for February 1888, and "189912" for December 1888. I would like to convert this numeric date format into a standard date format in R, specifically in the "YYYY-MM-DD" format.

I have tried using the standard date conversion functions in R, such as as.Date() and strptime(), but I couldn't find a direct way to handle this specific numeric date format. The desired output for the given examples would be "1899-01-01", "1888-02-01", and "1888-12-01", respectively.

I believe I need to extract the year, month, and day components from the numeric date and then create a date object with the appropriate formatting. However, I am not sure about the most efficient and accurate approach to accomplish this.

I would greatly appreciate any guidance or code examples that can help me achieve this conversion in R. Specifically, I am looking for suggestions on how to extract the year, month, and day components from the numeric date and then create a valid date object with the desired format.

Thank you in advance for your assistance and expertise.

Arvind Reddy
  • 406
  • 2
  • 10

1 Answers1

1
date_nums <- paste(c("189901", "189902", "189912"), "01", sep = "")
date_format <- as.Date(paste(date_nums, "01"), format = "%Y%m%d")

date_format
[1] "1899-01-01" "1899-02-01" "1899-12-01"
Eugene Chong
  • 1,591
  • 5
  • 16