3

Below is a reproducible example showing the problem:

openSummary <- read.table(textConnection(
"Dates          dollarA         numTotal
7/3/2011    52730.56    1614
7/10/2011   77709.43    1548"), header = TRUE)
openSummary$Dates <- strptime(openSummary$Dates,"%m/%d/%Y")
str(openSummary)
head(openSummary) # No problem

openSummaryDT <- data.table(openSummary)
str(openSummaryDT)
head(openSummaryDT) # An error is produced

Here is the error upon executing head(openSummaryDT)

Error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE),  : 
length of 'dimnames' [1] not equal to array extent 

please explain the error and how can I avoid it. However, it appears that i can do some operation on both data frame and data table and I get the same results.

difftime(Sys.Date(), openSummary[ ,"Dates"])
difftime(Sys.Date(), openSummaryDT[ ,Dates])

Thank you in advance

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
Ragy Isaac
  • 1,458
  • 1
  • 17
  • 22

1 Answers1

5

This is a fascinating bug caused by the dates being in POSIXlt format. Consider:

openSummary$Dates <- as.Date(openSummary$Dates)
head(data.table(openSummary))
#         Dates  dollarA numTotal
# 1: 2011-07-03 52730.56     1614
# 2: 2011-07-10 77709.43     1548

If you try to print the original table, you get the same error, but with a traceback, you see this:

> openSummaryDT
# Error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE),  : 
#  length of 'dimnames' [1] not equal to array extent
# In addition: Warning message:
#  In cbind...
# Enter a frame number, or 0 to exit   
# 1: print(list(Dates = list(sec = c(0, 0), min = c(0, 0), hour = c(0, 0), m
# 2: print.data.table(list(Dates = list(sec = c(0, 0), min = c(0, 0), hour =
# 3: `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE), ":", sep 
> 3
# Called from: print.data.table(list(Dates = list(sec 
Browse[1]> ls()
# [1] "dn"    "value" "x"   
Browse[1]> x
#       Dates     dollarA    numTotal
# sec   "0,0"     "52730.56" "1614"  
# min   "0,0"     "77709.43" "1548"  
# hour  "0,0"     "52730.56" "1614"  
# mday  "3,10"    "77709.43" "1548"  
# mon   "6,6"     "52730.56" "1614"  
# year  "111,111" "77709.43" "1548"  
# wday  "0,0"     "52730.56" "1614"  
# yday  "183,190" "77709.43" "1548"  
# isdst "1,1"     "52730.56" "1614"  

Basically, for whatever reason, the process of converting data.table into text form for printing / heading exposes the underlying list/vector nature of the POSIXlt object.

BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • 2
    Currently POSIXlt columns are not supported by `data.table` (see [here](http://stackoverflow.com/a/14063077/1385941)). – mnel Jan 23 '14 at 22:32
  • 1
    @mnel It seems like it should error out then, no? Thanks for the link, I didn't realize that (though I've never had a need to use `POSIXlt`. – BrodieG Jan 23 '14 at 22:35