Wish fastPOSIXct
works - but not working in this case.
Here is my time data (which does not have dates) - and I need to get the hours-part from them.
times <- c("9:46","11:06", "14:17", "19:53", "0:03", "3:56")
Here is the wrong output from fastPOSIXct
:
fastPOSIXct(times, "GMT")
[1] "1970-01-01 00:00:00 GMT" "1970-01-01 00:00:00 GMT"
[3] "1970-01-01 00:00:00 GMT" "1970-01-01 00:00:00 GMT"
[5] "1970-01-01 00:00:00 GMT" "1970-01-01 00:00:00 GMT"
It does not recognize the times without the presence of dates correctly.
The hour
method from data.table
with as.ITime
solves the purpose, but looks like slow on large times arrays.
library(data.table)
hour(as.ITime(times))
# [1] 9 11 14 19 0 3
Wondering if there is some faster way (just like fastPOSIXct
, but works without the need for date).
fastPOSIXct
really works like snap, but just wrong.
You can use the
stri_sub
function from the stringi package and trim the last 3 characters like this:If
from
and/orto
parameters are negative then counting is done from the end of a string. So in this example the substring is from the first character to the fourth one but counting from the end of string.Is this an option? This is a
base
solution.You may also try
substr
:as.integer(substr(vals, start = 1, stop = nchar(vals) - 3))
In a benchmark on a vector with 10e6 elements,
stringi::stri_sub
is fastest, andsubstr
number two.You can also do this with the
times
function from thechron
package:If speed is important, you could extract the hours more quickly with a string manipulation:
times
andas.POSIXlt
(from @tonytonov's solution) seem to be somewhat quicker thanas.ITime
, and the string manipulation is much quicker:To really speed up, you can also just trim off the lsat 3 chars from the strings. It's faster than using
regex
.Here are benchmark results
str_sub
orsubstr
will always be handy in this situation. For example, the following code is forsubstr
: