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

I have what, in my opinion, is a relatively mild change 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 execute () 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 is an example of code that accurately reproduces the problem. To keep the question short, I put data 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 causes the error? I thought this was a fairly simple permutation.

+3
source share
1 answer

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

  • Use reshape2is an updated version, which is also faster.
  • dcast - reshape2, data.frame, acast, .

:

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

:

        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
+4

All Articles