0

My data set truncates the seconds if it is :00 For example :

Time_TS
2022-06-22T18:48:58
2022-06-22T18:48:59
2022-06-22T18:49
2022-06-22T18:49:01
2022-06-22T18:49:59
2022-06-22T18:50
2022-06-22T18:50:01
2022-06-22T18:50:02

Hence rows 3 and 6 result in an NA when I use the strptime fnction:

source1$Time_TSS<-strptime(source1$Time_TS, '%Y-%m-%dT%H:%M:%S')

I tried the method suggested in this post, but it doesn't seem to work. Any other ideas?

thentangler
  • 1,048
  • 2
  • 12
  • 38
  • You could probably just paste ```:00``` for any Time_TS that is under the desired character length (19 I think?) – Silentdevildoll Jun 23 '22 at 19:34
  • @Silentdevildoll I thought about that, but dint want to take the risk of missing some edge cases where it would be different characters. But I guess im just being paranoid :P – thentangler Jun 24 '22 at 13:25

1 Answers1

1
dat$Time_TS <- gsub("(T\\d{2}:\\d{2})$", "\\1:00", dat$Time_TS)
dat
#               Time_TS
# 1 2022-06-22T18:48:58
# 2 2022-06-22T18:48:59
# 3 2022-06-22T18:49:00
# 4 2022-06-22T18:49:01
# 5 2022-06-22T18:49:59
# 6 2022-06-22T18:50:00
# 7 2022-06-22T18:50:01
# 8 2022-06-22T18:50:02
strptime(dat$Time_TS, '%Y-%m-%dT%H:%M:%S')
# [1] "2022-06-22 18:48:58 EDT" "2022-06-22 18:48:59 EDT"
# [3] "2022-06-22 18:49:00 EDT" "2022-06-22 18:49:01 EDT"
# [5] "2022-06-22 18:49:59 EDT" "2022-06-22 18:50:00 EDT"
# [7] "2022-06-22 18:50:01 EDT" "2022-06-22 18:50:02 EDT"
r2evans
  • 141,215
  • 6
  • 77
  • 149
  • FYI, if you're confident enough in the rest of the strings, one could also use `paste0(dat$Time_TS, ifelse(nchar(dat$Time_TS) == 16, ":00", ""))`. – r2evans Jun 24 '22 at 15:28
  • 1
    I did use the `paste0` but I would prefer the reg ex since it would take anything of any length. – thentangler Jun 24 '22 at 19:54