开发者

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

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开发者_如何学C 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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜