Reputation: 359
I have this column in a data frame
> str(df[,"Saida"])
POSIXct[1:2498938], format: "2016-01-01 04:05:00" "2016-01-01 04:23:00" "2016-01-02 04:09:00" "2016-01-03 03:58:00" ...
I would like to ignore the date, when the event happened, but just consider the hour, minute and second. I though it was straight forward:
hms(df[,"Saida"])
And this appears
Warning message:
In .parse_hms(..., order = "HM", quiet = quiet) :
Some strings failed to parse
Not good sign. The return was a vector filled with NA
------------------------------------------------------------------------
Could you please help me find a solution for, eg: Convert "2016-01-01 04:05:00" to "04:05:00" as a time class, so I could take the difference between the elements, latter on
PS: This could be helpful
> dput(head(df[,"Saida"]))
structure(c(1451628300, 1451629380, 1451714940, 1451800680, 1451892720,
1451973420), class = c("POSIXct", "POSIXt"), tzone = "")
Upvotes: 2
Views: 631
Reputation: 887601
Here is another option with times
from chron
library(chron)
times(format(vec, "%H:%M:%S"))
#[1] 11:35:00 11:53:00 11:39:00 11:28:00 13:02:00 11:27:00
Upvotes: 2
Reputation: 37889
This is one way to do it using data.table
:
library(data.table)
#I named your dput(head(df[,"Saida"])) as vec
time <- as.ITime(vec)
time
#[1] "06:05:00" "06:23:00" "06:09:00" "05:58:00" "07:32:00" "05:57:00"
str(time)
#Class 'ITime' int [1:6] 21900 22980 22140 21480 27120 21420
As you can see the class is ITime
so that you can do any calculations you want later.
Upvotes: 5