Help getting chr date mm/dd/yyyy hh::mm:ss to yyyy-mm-dd hh:mm:ss

I have 11 CSV sheets that are 100k rows or more that have a chr format mm/dd/yyyy hh::mm:ss and one file where the dates are what i need dttm yyyy-mm-dd hh:mm:ss

Before I combine these files I am trying to fix the chr dates to dttm so that i can then use them in my calculations. I can't figure out how to do this and would appreciate any help!

Is this the sort of thing your need?

DF <- data.frame(DateTime = c("02/13/2023 13:42:10", "04/30/2022 06:01:32"),
                 Value = 1:2)
DF
#>              DateTime Value
#> 1 02/13/2023 13:42:10     1
#> 2 04/30/2022 06:01:32     2
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
DF$DateTime <- mdy_hms(DF$DateTime)
DF
#>              DateTime Value
#> 1 2023-02-13 13:42:10     1
#> 2 2022-04-30 06:01:32     2

Created on 2024-03-11 with reprex v2.0.2

How would i apply that to updating 250k rows? Below are the started_at and ended_at data from two files. the first is one I want to change to be like the second. And for the record I am a newbie. I have figured out other things on my own but this one is throwing me.

Rows: 258,678
Columns: 13
$ ride_id            <chr> "6842AA605EE9FBB3", "F984267A75B99A8C", "FF7CF57CFE026D02", "6B61B916032CB6D6", "E55E61A5F1260040", "123AAD676850F53C", "592…
$ rideable_type      <chr> "electric_bike", "electric_bike", "classic_bike", "classic_bike", "electric_bike", "classic_bike", "classic_bike", "docked_b…
$ started_at         <chr> "3/16/2023 8:20", "3/4/2023 14:07", "3/31/2023 12:28", "3/22/2023 14:09", "3/9/2023 7:15", "3/22/2023 17:47", "3/8/2023 19:5…
$ ended_at           <chr> "3/16/2023 8:22", "3/4/2023 14:15", "3/31/2023 12:38", "3/22/2023 14:24", "3/9/2023 7:26", "3/22/2023 18:01", "3/8/2023 20:0…

and i need it to be

Rows: 426,590
Columns: 13
$ ride_id            <chr> "8FE8F7D9C10E88C7", "34E4ED3ADF1D821B", "5296BF07A2F77CB5", "40759916B76D5D52", "77A96F460101AC63", "8D6A2328E19DC168", "C97…
$ rideable_type      <chr> "electric_bike", "electric_bike", "electric_bike", "electric_bike", "electric_bike", "electric_bike", "electric_bike", "elec…
$ started_at         <dttm> 2023-04-02 08:37:28, 2023-04-19 11:29:02, 2023-04-19 08:41:22, 2023-04-19 13:31:30, 2023-04-19 12:05:36, 2023-04-19 12:17:3…
$ ended_at           <dttm> 2023-04-02 08:41:37, 2023-04-19 11:52:12, 2023-04-19 08:43:22, 2023-04-19 13:35:09, 2023-04-19 12:10:26, 2023-04-19 12:21:3…

The number of rows does not affect the code. If your data frame is named DF

library(lubridate)
DF$started_at <- mdy_hm(DF$started_at)
DF$ended_at <- mdy_hm(DF$ended_at)

Notice I used the mdy_hm() function because started_at and ended_at do not have a seconds value in the character representation of the date-time

1 Like

THANK YOU !!!!! I was totally overthinking it and this was so simple. Lesson learned!

This topic was automatically closed 21 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.