0

I have a large date-time series (%Y-%m-%d %H:%M:%S) that reads as follows:

[1] 2009-10-16 00:04:30 2009-10-16 00:04:40 2009-10-16 00:04:50 2009-10-16 00:05   
[5] 2009-10-16 00:05:10 2009-10-16 00:05:20

Basically the time increments by 10 secs in each step. However, at the whole minute the :00 is missing and is causing issues when I convert datetime to as.POSIXct. (I get NA).

How can I rectify this?

NicE
  • 21,165
  • 3
  • 51
  • 68
  • you can use a regexp to add the missing 0s or use a regexp to add the missing 0s `gsub("(.* [[:digit:]]{2}:[[:digit:]]{2}$)","\\1:00",data)`, if data is your vector – NicE Feb 28 '15 at 19:20

1 Answers1

1

I'd probably just test the length of the string...

a <- c("2009-10-16 00:04:30", "2009-10-16 00:04:40", "2009-10-16 00:04:50", "2009-10-16 00:05", "2009-10-16 00:05:10", "2009-10-16 00:05:20")
nchar(a)
[1] 19 19 19 16 19 19
a[nchar(a)==16] <- paste0(a[nchar(a)==16], ":00")
a
[1] "2009-10-16 00:04:30" "2009-10-16 00:04:40" "2009-10-16 00:04:50"
[4] "2009-10-16 00:05:00" "2009-10-16 00:05:10" "2009-10-16 00:05:20"
cory
  • 6,529
  • 3
  • 21
  • 41
  • I think that's the most straight-forward. I sometimes use `sprintf` to insert leading 0's but I've never tried it for trailing characters. – IRTFM Feb 28 '15 at 19:21
  • Thanks a million for your help and speedy reply. Your suggestions solved my issue. – user3919413 Feb 28 '15 at 19:44