Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error creating R data.table with date-time POSIXlt

Tags:

r

data.table

Problem creating data.table with date-time column:

> mdt <- data.table(id=1:3, d=strptime(c("06:02:36", "06:02:48", "07:03:12"), "%H:%M:%S"))
> class(mdt)
[1] "data.table" "data.frame"
> print(mdt)
Error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE),  : 
  length of 'dimnames' [1] not equal to array extent

Enter a frame number, or 0 to exit   

1: print(list(id = 1:3, d = list(sec = c(36, 48, 12), min = c(2, 2, 3), hour = c(6, 6, 7), mday = c(31,
2: print.data.table(list(id = 1:3, d = list(sec = c(36, 48, 12), min = c(2, 2, 3), hour = c(6, 6, 7), m
3: `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE), ":", sep = ""))

Create as data.frame and convert to data.table works!

> mdf <- data.frame(id=1:3, d=strptime(c("06:02:36", "06:02:48", "07:03:12"), "%H:%M:%S"))
> print(mdf)
  id                   d
1  1 2014-01-31 06:02:36
2  2 2014-01-31 06:02:48
3  3 2014-01-31 07:03:12
> mdt <- as.data.table(mdf)
> print(mdt)
   id                   d
1:  1 2014-01-31 06:02:36
2:  2 2014-01-31 06:02:48
3:  3 2014-01-31 07:03:12
> class(mdt)
[1] "data.table" "data.frame"

Am I missing anything or is it bug? If a bug, where do I report it?

Note I use R version 3.0.0 and I see some warnings re. packages built with version 3.0.2. Can it be the problem? Should I upgrade R itself? Everything else I do seems to be working though.

like image 745
Patrick Avatar asked Jan 31 '14 18:01

Patrick


People also ask

What is the difference between POSIXct and POSIXlt and as date?

The builtin as. Date function handles dates (without times); the contributed library chron handles dates and times, but does not control for time zones; and the POSIXct and POSIXlt classes allow for dates and times with control for time zones.

How do I convert time to data in R?

To convert characters to time objects in R, use the strptime() function. To convert time objects to characters in R, use the strftime() function.

What is POSIXct format?

POSIXct stores date and time in seconds with the number of seconds beginning at 1 January 1970. Negative numbers are used to store dates prior to 1970. Thus, the POSIXct format stores each date and time a single value in units of seconds. Storing the data this way, optimizes use in data.

What is POSIXct POSIXt?

class(datetime) ## [1] "POSIXct" "POSIXt" POSIXt is a virtual class which cannot be used directly. “A virtual class 'POSIXt' exists from which both of the classes inherit: it is used to allow operations such as subtraction to mix the two classes.”


1 Answers

Formatting response from Blue Magister's comment (thanks so much), data.table does not support POSIXlt data types for performance reason -- see cast string to IDateTime as suggested as possible duplicate.

So the way to go is to cast time as ITime (type provided by data.table) or date-time (or date only) as POSIXct, depending upon whether date info is important or not:

> mdt <- data.table(id=1:3, d=as.ITime(strptime(c("06:02:36", "06:02:48", "07:03:12"), "%H:%M:%S")))
> print(mdt)
   id        d
1:  1 06:02:36
2:  2 06:02:48
3:  3 07:03:12
> mdt <- data.table(id=1:3, d=as.POSIXct(strptime(c("06:02:36", "06:02:48", "07:03:12"), "%H:%M:%S")))
> print(mdt)
   id                   d
1:  1 2014-01-31 06:02:36
2:  2 2014-01-31 06:02:48
3:  3 2014-01-31 07:03:12

As an extra note in case someone can benefit from it, I wanted to create date & time from my input data with date & time in separate fields. I found it useful to learn (see ?ITime) that one can add time ITime to date-time POSIXct and get a date-time POSIXct as follows:

> mdt <- as.POSIXct("2014-01-31") + as.ITime("06:02:36")
> print(mdt)
[1] "2014-01-31 06:02:36 EST"
> class(mdt)
[1] "POSIXct" "POSIXt" 
like image 135
Patrick Avatar answered Oct 27 '22 06:10

Patrick