I have a dataframe which contains multiple columns with date-values as strings (format: YYYYMMDD).
dt_example <- data.frame (
W03 = "20201130",
W44 = "19711031",
P01 = "19740813",
P04 = "20000506",
Z02 = "20201231"
)
Now I want to convert the strings to date-format and tried the following code which works fine:
dt_example_date <- dt_example %>%
mutate(
W03 = as.Date(W03, "%Y%m%d")
,W44 = as.Date(W44, "%Y%m%d")
,P01 = as.Date(P01, "%Y%m%d")
,P04 = as.Date(P04, "%Y%m%d")
,Z02 = as.Date(Z02, "%Y%m%d")
)
But this code is annoying and I want to use "mutate across" instead of mutliple as.Date-conversions.
I've tried several alternatives, but nothing works.
dt_example_date_2 <- dt_example %>%
mutate(
across(
c("W03","W44","P01","P04","Z02")
,as.Date
)
)
Result: character string is not in a standard unambiguous format
dt_example_date_2 <- dt_example %>%
mutate(
across(
c("W03","W44","P01","P04","Z02")
,as.Date(format="%y%m%d")
)
)
Result: argument "x" missing
dt_example_date_2 <- dt_example %>%
mutate(
across(
c("W03","W44","P01","P04","Z02")
,as.Date(., format="%y%m%d")
)
)
Result: do not know how to convert '.' to class “Date”
I didn't get it and don't know, what to do.
How can I pass paramaters to the function used within the across-command?
Greetings Benne