2013-01-06 112 views
0

我想融化一個數據框,並得到這個奇怪的錯誤。任何想法爲什麼?R - 重塑 - 融化錯誤

str(zx7) 
'data.frame': 519 obs. of 5 variables: 
$ calday.new: Date, format: "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" ... 
$ A20 : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... 
$ B20 : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... 
$ C20 : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... 
$ D20 : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... 

zx7.melt <- melt(zx7, id=c("calday.new")) 
Error in `[<-.ts`(`*tmp*`, ri, value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, : only replacement of elements is allowed 
+0

@你使用哪種版本的reshape2?我無法重現錯誤。 – agstudy

+0

@agstudy,正確的問題是「你使用了哪個版本的重塑?」看到我的迴應。 – A5C1D2H2I1M1N2O1R2T1

回答

2

我卻不知道你是如何創建的結構,但我這樣做的時候,我的作品

zx7 <- data.frame(calday.new=seq(from = as.Date('2011-01-03'),by=1,length.out=519), 
        A20=ts(rep(0,519)), 
        B20=ts(rep(0,519)), 
        C20=ts(rep(0,519)), 
        D20=ts(rep(0,519))) 

我創建相同的結構如上:

str(zx7) 
'data.frame': 519 obs. of 5 variables: 
$ calday.new: Date, format: "2011-01-03" "2011-01-04" "2011-01-05" ... 
$ A20  : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... 
$ B20  : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... 
$ C20  : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... 
$ D20  : Time-Series from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ... 

然後我融化:

head(melt(zx7, id=c("calday.new"))) 
    calday.new variable value 
1 2011-01-03  A20  0 
2 2011-01-04  A20  0 
3 2011-01-05  A20  0 
4 2011-01-06  A20  0 
5 2011-01-07  A20  0 
6 2011-01-08  A20  0 
+0

你也得到了我的+1,因爲你花時間重新創建了數據集,並且你對原始問題的評論在我腦海中引出了一個標誌:「誰對'reshape2'有什麼要求?」工作很好。 – A5C1D2H2I1M1N2O1R2T1

2

問題是: t舊的「重塑」包melt()函數在遇到類ts的對象時不知道該怎麼做。

所以,你有兩個明顯的選擇(儘管可能有更多):

  1. unclass當前歸類爲ts你以前melt()數據的變量:

    zx7b <- zx7   # Make a backup, just in case 
    library(reshape) # Notice this is "reshape", not "reshape2" 
    head(melt(zx7b, id=c("calday.new"))) # Doesn't work 
    # Error in `[<-.ts`(`*tmp*`, ri, value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, : 
    # only replacement of elements is allowed 
    
    ## Unclass the relevant columns from your data.frame 
    zx7b[sapply(zx7b, is.ts)] <- lapply(zx7b[sapply(zx7b, is.ts)], 
                unclass) 
    head(melt(zx7b, id=c("calday.new"))) 
    # calday.new variable value 
    # 1 2011-01-03  A20  0 
    # 2 2011-01-04  A20  0 
    # 3 2011-01-05  A20  0 
    # 4 2011-01-06  A20  0 
    # 5 2011-01-07  A20  0 
    # 6 2011-01-08  A20  0 
    
  2. 升級而不是「reshape2」,並且不需要unclassing。

    library(reshape2) # Notice that this is reshape2! 
    head(melt(zx7, id=c("calday.new"))) # Melt the original data.frame 
    # calday.new variable value 
    # 1 2011-01-03  A20  0 
    # 2 2011-01-04  A20  0 
    # 3 2011-01-05  A20  0 
    # 4 2011-01-06  A20  0 
    # 5 2011-01-07  A20  0 
    # 6 2011-01-08  A20  0 
    

我還沒有花時間去做,但你可以從「重塑」包的每個版本檢查爲melt.data.frame方法的代碼爲melt(),看看那裏的差別所在。安裝這兩個軟件包,然後鍵入reshape2:::melt.data.framereshape:::melt.data.frame以查看基本功能。

+0

+1!首先,我總是發現爲包名添加一個數字(重塑,rowygen,..)不是很優雅和混亂的來源!我希望這將被棄用,以避免這樣的錯誤!第二我會叫你先生重塑!你甚至說服我去掌握基本包的重塑功能! – agstudy

+0

@agstudy,我同意更改軟件包的名稱。我明白*爲什麼*已經完成,但是有兩個包含相似但不完全相同的功能和語法的包可能會引起混淆。 – A5C1D2H2I1M1N2O1R2T1