R reshape package: "Error in Dim(x)"... "dims [product 100] do not match the length of object [109]"

11,004

I don't know what's causing the error, but here is how you should fix it:

  1. Use reshape2 - this is an updated version which is also faster.
  2. Use dcast - the cast function in reshape2 which explicitly returns a data.frame, as opposed to acast that returns an array.

The code:

library(reshape2)
castDf <- dcast( meltDf , DATE + CITY ~ variable)
castDf

The results:

        DATE   CITY DAILY_MAX_TEMP
1 1953-01-01 Anqing            9.1
2 1953-01-02 Anqing            5.1
3 1953-01-03 Anqing            5.2
4 1953-01-04 Anqing            4.6
5 1953-01-05 Anqing            7.9
6 1953-01-06 Anqing            9.9
Share:
11,004

Related videos on Youtube

JD Long
Author by

JD Long

Only slightly ashamed creator of disgusting and frustrating code. I'm a data guy not a programmer. But sometimes I have to program my data into submission.

Updated on June 04, 2022

Comments

  • JD Long
    JD Long almost 2 years

    I've got what I think is a relatively benign reshape going using the reshape package. I have "molten" data that looks like this:

    > head(meltDf)
        CITY       DATE       variable value
    1 Anqing 1953-01-01 DAILY_MAX_TEMP   9.1
    2 Anqing 1953-01-02 DAILY_MAX_TEMP   5.1
    3 Anqing 1953-01-03 DAILY_MAX_TEMP   5.2
    4 Anqing 1953-01-04 DAILY_MAX_TEMP   4.6
    5 Anqing 1953-01-05 DAILY_MAX_TEMP   7.9
    6 Anqing 1953-01-06 DAILY_MAX_TEMP   9.9
    > str(meltDf)
    'data.frame':   100 obs. of  4 variables:
     $ CITY    : chr  "Anqing" "Anqing" "Anqing" "Anqing" ...
     $ DATE    : POSIXlt, format: "1953-01-01" "1953-01-02" "1953-01-03" "1953-01-04" ...
     $ variable: Factor w/ 1 level "DAILY_MAX_TEMP": 1 1 1 1 1 1 1 1 1 1 ...
     $ value   : num  9.1 5.1 5.2 4.6 7.9 9.9 8.1 13.3 17.6 17.6 ...
    

    But when I try to cast() the data I get this error:

    > castDf <- cast( meltDf , DATE + CITY ~ variable)
    Error in dim(X) <- c(n, length(X)/n) : 
      dims [product 100] do not match the length of object [109]
    

    Here's some example code that exactly reproduces the problem. In order to keep the question concise I put the data up on github:

    require(RCurl)
    require(reshape)
    myFile <- getURL("https://raw.github.com/gist/1010735/29ec65a48740ebe512f8af7a124e1e65e91ac054")
    temporaryFile <- tempfile()
    con <- file(temporaryFile, open = "w")
    cat(myFile, file = con) 
    close(con)
    meltDf <- dget(temporaryFile)
    castDf <- cast( meltDf , DATE + CITY ~ variable)
    

    Any ideas what is causing the error? I thought this was a pretty straightforward reshape.

    • Prasad Chalasani
      Prasad Chalasani almost 13 years
      I found that the error goes away if you do meltDF$DATE <- as.Date(meltDF$DATE). I still haven't fathomed all of the POSIX** stuff, but that seems to fix it. Or just use reshape2 as @Andrie suggests.
    • JD Long
      JD Long almost 13 years
      Ohhhhh... damn dates. I tried everything except changing date formats. I changed case of variables, order, etc. I should have tried that.