Recommended Date R-Table

I have a csv file with one timestamp column "2000-01-01 12: 00: 00.123456". What is the recommended way to handle this in a data table? I need to deal with grouping, matching / sliding with an IDate column from another table, building time series, 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 a possible duplicate question in which Matthew suggested manually pour dates into integers. But that is 3 years old, and I wonder if there is now a better way?

+5
source share
2 answers

IDateTime requires an object of the POSIXct class to work correctly (it looks like it works correctly with the factor transform, and is not sure why). I agree that it is not documented very well and it might be worth opening FR / PR on GH regarding documentation - there is an open queue regarding IDateTime vignette though. And FR already exists, which allows you to work with the 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 behavior


Regardless of the fact that the error is actually caused by the ITime print ITime , which calls format.ITime , see here and here for example, if you run res <- IDateTime("2015-09-29 08:22:00") , this will not result in an error, although res will be NA due to incorrect conversion (I believe) to here (format is only "%H:%M:%OS" ). It seems like a mistake to me, and I still don't know why the factor class works correctly if there is no factor method in methods(as.ITime) . Perhaps due to its integer storage mode, which calls another related method.

+7
source

Depending on the accuracy required for your time fields, you may need a POSIXct instead of an IDateTime .
The timestamp format stored in the source file can be played back in R to format(Sys.time(), "%Y-%m-%d %H:%M:%OS6") .
When using IDateTime you will lose subseconds, you can play with ITime and see if it is suitable for your needs.
If you stick with POSIXct , then you should be aware of the ?setNumericRounding function, which can be important sometimes, since it affects the ordering and joining of the POSIXct base numeric data type.

+2
source

Source: https://habr.com/ru/post/1233462/


All Articles