'Standardize Column with Different Date Types R
I am reading manually entered Dates from Excel into R. With the nature of manual entry, there are inevitably some different date types in the date column.
Sometimes, the Dates are entered in as: "4/22/2021" or "8/31/2021". Which when I read into R with read_excel translates to: "2021-04-22 UTC" and "2021-08-31 UTC".
But there are other dates in the same column with the numeric Excel date format, such as "44582" or "44603".
I am looping through excel files and can't bind them because of the differing data types.
Is there a way to standardize these dates?
Current data:
date
2021-04-22 UTC
2021-08-31 UTC
44582
44603
Desired output:
date
2021-04-22
2021-08-31
2022-01-21
2022-02-11
Solution 1:[1]
We could use
library(dplyr)
df1 %>%
mutate(date = case_when(is.na(as.numeric(date)) ~ as.Date(date),
TRUE ~ as.Date(as.numeric(date), origin = "1899-12-30")))
data
df1 <- structure(list(date = c("2021-04-22 UTC", "2021-08-31 UTC", "44582",
"44603")), class = "data.frame", row.names = c(NA, -4L))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | akrun |
