0

When i strip date and time using strptime()it converts my time from class POSIXct to character.

> head(data)
           timestamps value
1 2017-10-01 00:00:00 8.424
2 2017-10-01 01:00:00 7.832
3 2017-10-01 02:00:00 6.320
4 2017-10-01 03:00:00 6.696
5 2017-10-01 04:00:00 5.448
6 2017-10-01 05:00:00 4.992

> data$time <- format(data$timestamps,"%H:%M:%S")
> str(data)
'data.frame':   226 obs. of  3 variables:
 $ timestamps: POSIXct, format: "2017-10-01 00:00:00" "2017-10-01 01:00:00" "2017-10-01 02:00:00" ...
 $ value     : num  8.42 7.83 6.32 6.7 5.45 ...

$ time : chr "00:00:00" "01:00:00" "02:00:00" "03:00:00" ...

You can see it converts time to character. How do i strip time with same class POSIXct?

This link here gives how to convert it to Integer and take hours, minutes values alone. I want my timestamp to convert it to Posixct and Not Integer. Also read the line given in that link.I would not advise to do that if you want to do any further operations on your data. However, it might be convenient to do that if you want to plot the results.

And this doesn't work either : as.integer(format(Sys.time(), "%H%M%S"))

Jonreyan
  • 131
  • 2
  • 3
  • 14
  • Please refer this: https://stackoverflow.com/questions/31876962/date-time-conversion-and-extract-only-time – amrrs Oct 11 '17 at 11:24
  • 1
    Possible duplicate of [date time conversion and extract only time](https://stackoverflow.com/questions/31876962/date-time-conversion-and-extract-only-time) – amrrs Oct 11 '17 at 11:24
  • I have already seen that, It doesn't work for me – Jonreyan Oct 11 '17 at 11:29
  • Added my 2cents in the answer. Doesn't seem to have any other work around. – amrrs Oct 11 '17 at 11:44

1 Answers1

0

The only possible solution is to convert the character time into POSIXlt object (which would append current date to it)

> strptime('10:12:00',format='%H:%M:%S')
[1] "2017-10-11 10:12:00 IST"
> class(strptime('10:12:00',format='%H:%M:%S'))
[1] "POSIXlt" "POSIXt" 
> 

Alternatively, lubridate to individually extract Hour, Min, Seconds (as integers) and playing around with them!

amrrs
  • 6,215
  • 2
  • 18
  • 27
  • Thanks. But i would like to have the time alone. I don't want date to be included in that. Is there a way to convert `$ time : chr "00:00:00" "01:00:00" "02:00:00" "03:00:00" ...` to Posixct or posixlt – Jonreyan Oct 11 '17 at 11:48
  • Ideally, POSIX objects will always be with Date and Time. Ref: https://stackoverflow.com/a/10700025/5086335 – amrrs Oct 11 '17 at 11:51