Hello,
I have following format of dates in CSV file, which I want to change to 10 character date, format %y, %m, %d.
|04/27/2023|04/27/2023| = 10 chatater dates (excel)
|04/28/2023|04/28/2023|
|11/2022|12/2022| = 7 character dates (excel)
|01/2023|01/2023|
some values have 7 characters and others have 10. You can I use dplyr::case_when()
and nchar()
to build the logic to change the date to 10 characters and then to Format %y, %m, %d?
Thank you.
Below is one approach, which adds the first day of the month to the 7 character dates.
library(tidyverse)
d = data.frame(
date = c('04/27/2023', '04/28/2023', '12/2022', '01/2022')
)
d %>%
mutate(month = substr(date,1,2),
year = substr(date, nchar(date) - 1, nchar(date)),
day = ifelse(nchar(date) != 10, '01', substr(date,4,5))
) %>%
mutate(y_m_d = paste0(year, ', ', month, ', ', day)) %>%
select(date, y_m_d)
#> date y_m_d
#> 1 04/27/2023 23, 04, 27
#> 2 04/28/2023 23, 04, 28
#> 3 12/2022 22, 12, 01
#> 4 01/2022 22, 01, 01
Created on 2023-02-08 with reprex v2.0.2.9000
1 Like
Thank you Scotty , problem solved.
system
Closed
March 27, 2023, 7:08am
4
This topic was automatically closed 45 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.