Reputation: 2253
I have these times dd-mm-yyyy hh:mm
date
1 14-02-2020 21:02
2 30-04-2019 10:46
3 26-01-2019 10:10
4 26-04-2018 13:38
5 13-07-2017 10:24
6 03-02-2020 13:38
7 16-09-2020 19:35
8 10-02-2020 04:46
9 08-09-2017 13:34
10 08-09-2017 13:34
I want to format to dd-mm-yyyy
. I tried
p %>% mutate(new = format(as.Date(times, "%d-%m-%Y hh:mm"), "%d%m%y"))
But that did not work
p <- structure(list(times = c("14-02-2020 21:02", "30-04-2019 10:46",
"26-01-2019 10:10", "26-04-2018 13:38", "13-07-2017 10:24", "03-02-2020 13:38",
"16-09-2020 19:35", "10-02-2020 04:46", "08-09-2017 13:34", "08-09-2017 13:34"
)), row.names = c(NA, 10L), class = "data.frame")
Upvotes: 0
Views: 187
Reputation: 2021
Tons of ways to do it. I like the date one. But if you just want to strip things off a character vector you can do
p$times <- substr(p$times,1,10)
> p
times
1 14-02-2020
2 30-04-2019
3 26-01-2019
4 26-04-2018
5 13-07-2017
6 03-02-2020
7 16-09-2020
8 10-02-2020
9 08-09-2017
10 08-09-2017
Upvotes: 1
Reputation: 5908
As per your dput, the p$dates variable is in date and time format. When you parse it with as.Date()
, exclude the hour/minute/second signals.
p %>% mutate(dmy_date = as.Date(times, format = "%d-%m-%Y"))
times dmy_date
1 14-02-2020 21:02 2020-02-14
2 30-04-2019 10:46 2019-04-30
3 26-01-2019 10:10 2019-01-26
4 26-04-2018 13:38 2018-04-26
5 13-07-2017 10:24 2017-07-13
6 03-02-2020 13:38 2020-02-03
7 16-09-2020 19:35 2020-09-16
8 10-02-2020 04:46 2020-02-10
9 08-09-2017 13:34 2017-09-08
10 08-09-2017 13:34 2017-09-08
Upvotes: 2