I have a csv file with one column of timestamps "2000-01-01 12:00:00.123456". What's the recommended way to dealing with it in data table? I need to deal with grouping, matching/rolling join with IDate column from another table, time series plotting, etc.
IDateTime("2000-01-01 12:00:00.123456")
Error in if (any(neg)) res[neg] = paste("-", res[neg], sep = "") :
missing value where TRUE/FALSE needed
I see this answer in the possible duplicate question in which Matthew suggested manually casting dates into integers. But that's 3 years old and I wonder if there now exists a better way?
I'm often surprised that even those who are well familiar with R are not aware of this amazing R package called 'lubridate' from Garrett Grolemund, Vitalie Spinu, Hadley Wickham, etc., which provides a comprehensive set of functions to work with date and time related data more effectively and intuitively.
The default for the format methods is "%Y-%m-%d %H:%M:%S" if any element has a time component which is not midnight, and "%Y-%m-%d" otherwise. If options("digits. secs") is set, up to the specified number of digits will be printed for seconds.
and. time() method in R can be used to merge together the date and time to obtain date-time object in POSIX format. Parameter : date – Date can be specified either in the Date format or in the form of character string referred by “YYYY-MM-DD”.
IDateTime
requires a POSIXct
class object in order to work properly (it seems to work properly with a factor
conversion too, not sure why). I agree it isn't documented very well and maybe worth opening an FR/PR on GH regarding documentation- there is an open queue regarding an IDateTime
vignette though. And there is already an FR regarding allowing it to work with a character
class.
IDateTime(as.POSIXct("2000-01-01 12:00:00.123456"))
# idate itime
# 1: 2000-01-01 12:00:00
## IDateTime(factor("2000-01-01 12:00:00.123456")) ## will also work
Pay attention to the tz
parameter in as.POSIXct
if you want to avoid unexpected behaviour
Regardless, it seems like the error is actually caused by the print method of ITime
which calls format.ITime
, see here and here
e.g., if you will run res <- IDateTime("2015-09-29 08:22:00")
this will not produce an error, though res
will be NA
due to wrong conversion (I believe) in here (the format is only "%H:%M:%OS"
). It seems like a bug to me and I still uncertain why factor
class works correctly when there is no factor
method in methods(as.ITime)
. Maybe due to its integer
internal storage mode which calls another related method.
Depending on the precision required for your time fields you may need to use POSIXct
instead of IDateTime
.
The timestamp format stored in your source file can be reproduced in R by format(Sys.time(), "%Y-%m-%d %H:%M:%OS6")
.
When using IDateTime
you will lose the subseconds, you can play with ITime
and see if it fits your need.
If you will stick to POSIXct
then you should be aware of ?setNumericRounding
function which may be sometimes important as it affects ordering and joining on POSIXct
's underlying numeric data type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With