Hello, I need to convert a column of date and time which is character data type (eg: "2022-06-30 17:27:53" "2022-06-30 18:39:52" ) into seperate date and time column of datetime datatype.
I used
cyclist_yr$started_at_date <- as.Date(cyclist_yr$started_at) to create a table of date only. But I'm unable to create a table of time only.
I used
cyclist_yr$started_at_time <- as.POSIXct(cyclist_yr$started_at, format = "%Y/%m/%d %H:%M:%S", tz = "GMT") to convert it to POSIXct and then seperate it. But my output structure is like
started_at_time : POSIXct, format: NA NA NA ...
Please help me guys to seperate time from it.
Is this the sort of thing you are looking for?
DF <- data.frame(Datetime = c("2022-06-30 17:27:53", "2022-06-30 18:39:52"))
library(tidyr)
library(hms)
library(dplyr)
DF <- DF |> separate(col = "Datetime", into = c("Date","Time"),sep = " ", remove = FALSE) |>
mutate(Date = as.Date(Date), Time = as_hms(Time))
DF
#> Datetime Date Time
#> 1 2022-06-30 17:27:53 2022-06-30 17:27:53
#> 2 2022-06-30 18:39:52 2022-06-30 18:39:52
summary(DF)
#> Datetime Date Time
#> Length:2 Min. :2022-06-30 Length:2
#> Class :character 1st Qu.:2022-06-30 Class1:hms
#> Mode :character Median :2022-06-30 Class2:difftime
#> Mean :2022-06-30 Mode :numeric
#> 3rd Qu.:2022-06-30
#> Max. :2022-06-30
Created on 2023-07-14 with reprex v2.0.2
1 Like
Thank You so much dude.... I had been trying to solve this for the whole day
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.