Reputation: 11
start_end$started_at
[1] "2/7/22 15:47" "3/21/22 20:05" "3/22/22 7:24" "3/5/22 12:49"
"1/10/22 8:24" "1/23/22 7:47"
start_end$start_time <- as.POSIXct(as.numeric(as.character(start_end$started_at)),
format="%H:%M",tz="UTC")
I keep getting NA output when attempting to convert char to numeric or time format.
I know that column started_at
needs to be converted from character to numeric which is why I used as.numeric
and as.character
but I keep getting NA coercion. I've tried as.POSIXct
and strptime
.
Warning message: In as.POSIXct(as.numeric(as.character(start_end$started_at)), format = "%H:%M", : NAs introduced by coercion
Also Tried:
StartTime2 <- strptime(start_end$started_at, "%H:%M:%S")
I've tried these so far
start_end$start_time <- as.POSIXct(as.numeric(as.character(start_end$started_at)),
format="%H:%M",tz="UTC")
StartTime2 <- strptime(start_end$started_at, "%H:%M:%S")
Upvotes: 1
Views: 159
Reputation: 18734
This is one of those times when you can make it a challenge or really easy. The library lubridate
is phenomenal for dates. Notice the function, mdy_hm
; I bet you can tell what it means: month, day, year, hour, minutes.
library(lubridate)
sta <- c("2/7/22 15:47", "3/21/22 20:05", "3/22/22 7:24", "3/5/22 12:49",
"1/10/22 8:24", "1/23/22 7:47")
sta2 <- mdy_hm(sta)
# [1] "2022-02-07 15:47:00 UTC" "2022-03-21 20:05:00 UTC" "2022-03-22 07:24:00 UTC"
# [4] "2022-03-05 12:49:00 UTC" "2022-01-10 08:24:00 UTC" "2022-01-23 07:47:00 UTC"
As you can see, it defaulted to Greenwich time. You can set your time zone, as well.
sta3 <- mdy_hm(sta, tz = "EST")
# [1] "2022-02-07 15:47:00 EST" "2022-03-21 20:05:00 EST" "2022-03-22 07:24:00 EST"
# [4] "2022-03-05 12:49:00 EST" "2022-01-10 08:24:00 EST" "2022-01-23 07:47:00 EST"
Upvotes: 0