0

I need to create a date column and a time column from a date time field.

The structure of the data:

Date.Time: POSIXct, format: "2017-04-01 05:17:02" "2017-04-01 05:18:20" "2017-04-01 05:25:24" "2017-04-01 05:31:46" .. data:

I made this entry in R

hour_min <- format(as.POSIXct(strptime(FitFull$Date.Time,"%d/%m/%Y %H:%M",tz="",format = %H:%M)))

And got this error message:

Error: unexpected SPECIAL in "hour_min <- format(as.POSIXct(strptime(FitFull$Date.Time,"%d/%m/%Y %H:%M",tz="",format = %H:%"

Where can I go from here? "New R User"

Thanks for any help you can give me.

  • Possible duplicate of https://stackoverflow.com/questions/21585138/extracting-time-from-character-string-with-strptime-in-r-returning-na – akrun Jun 01 '17 at 10:19

1 Answers1

0

You've made several mistakes:

  • The format in strptime is wrong. Is should be: "%Y-%m-%d %H:%M".
  • You don't need strptime and as.POSIXct. One of them is sufficient.
  • The format-part of format is in the wrong spot and the format itself needs quotation-marks around it.

The correct specification is:

format(strptime("2017-04-01 05:17:02", "%Y-%m-%d %H:%M:%S"), format = "%H:%M")

which gives:

[1] "05:17"

HTH

Jaap
  • 81,064
  • 34
  • 182
  • 193