jarle
October 12, 2023, 7:00am
1
Hi
I have ended up with a column in a dataframe with dates in different formats. How should I go about making the last two similar to the first two, i.e. change intervdate format from %y-%m-%d to %d.%m.%y ?
The real dataframe consists of multiple different dataframes (from csv files that for some reason have different date formats)
Thanks for pointing me in the right direction!
df <- data.frame(id = c(1:4),
intervdate = c("2023-07-25", "2023-06-14", "31.01.2020", "25.02.2020"))
You would be better off using as.Date()
to convert them separately, and then combine and set format with
format(dates, format = "%d.%m.%Y")
which allows performing date arithmetic.
However, to just conform the string representation
d <- data.frame(
id = c(1:4),
date = c("2023-07-25", "2023-06-14", "31.01.2020", "25.02.2020"))
reformat <- function(x) gsub("(\\d{4})-(\\d{2})-(\\d{2})", "\\3.\\2.\\1", x)
d[grep("^\\d{4}-",d$date),"date"] <- reformat(d[grep("^\\d{4}-",d$date)",date"])
d
#> id date
#> 1 1 25.07.2023
#> 2 2 14.06.2023
#> 3 3 31.01.2020
#> 4 4 25.02.2020
Created on 2023-10-12 with reprex v2.0.2
DavoWW
October 12, 2023, 12:38pm
3
Hi @jarle
You could try the package anytime
which tries to automatically handle a multitude of date formats.
jarle
October 12, 2023, 3:04pm
4
Thanks!
This solved it , much appreciated!
1 Like
jarle
October 12, 2023, 3:04pm
5
Thanks! Will try that eventually
system
Closed
October 19, 2023, 3:05pm
6
This topic was automatically closed 7 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.