I have a dataframe imported from excel with read_excel
that looks like this:
The main task is to handle the different formats of dates:
I would like to implement it into a custom function (and I am not good at all in creating functions):
df <- structure(list(date = c("40574", "40861", "40870", "40990", "07.03.2022",
"14.03.2022", "16.03.2022", "27.03.2022", "24.03.2022", "24.03.2022"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
# A tibble: 10 x 1
date
<chr>
1 40574
2 40861
3 40870
4 40990
5 07.03.2022
6 14.03.2022
7 16.03.2022
8 27.03.2022
9 24.03.2022
10 24.03.2022
I solved this task with this code:
library(tidyverse)
library(janitor)
library(lubridate)
df %>%
mutate(new_col = excel_numeric_to_date(as.numeric(as.character(date)), date_system = "modern"), .before=1) %>%
mutate(date = ifelse(!str_detect(date, '\\.'), NA_character_, date)) %>%
mutate(date = dmy(date)) %>%
mutate(date = coalesce(date, new_col), .keep="unused")
From this code I would like to make a custom function with this code:
mixed_dateColumn_excel <- function(df, x) {
x <- {{x}}
df %>%
mutate(new_col = excel_numeric_to_date(as.numeric(as.character(x)), date_system = "modern"), .before=1) %>%
mutate(x = ifelse(!str_detect(x, '\\.'), NA_character_, x)) %>%
mutate(x = dmy(x)) %>%
mutate(x = coalesce(x, new_col), .keep="unused")
}
I would like to know why:
This works not:
mixed_dateColumn_excel(df, "date")
This works not also:
mixed_dateColumn_excel(df, date)
And this works:
mixed_dateColumn_excel(df, df$date)