2012-10-20 92 views
11

我有data.frame,我可以積用matplot:'dimnames'[1]的長度不等於數組範圍'是什麼意思?

> dim(lhbyzone) 
[1] 38070 21 
> matplot(lhbyzone) 
(no error occurs here) 

但如果我把LH的頭部,並嘗試,它給了我一個奇怪的錯誤:

> foo <- head(lhbyzone,1000) 
> matplot(foo) 
Error in `rownames<-`(`*tmp*`, value = c("1", "2", "3", "4", "5", "6", : 
    length of 'dimnames' [1] not equal to array extent 

所以,可能與名字有關?

> dimnames(foo) 
[[1]] 
    [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" 
    [11] "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" 
    [21] "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" 
    [31] "31" "32" "33" "34" "35" "36" "37" "38" "39" "40" 
... 
[951] "951" "952" "953" "954" "955" "956" "957" "958" "959" "960" 
[961] "961" "962" "963" "964" "965" "966" "967" "968" "969" "970" 
[971] "971" "972" "973" "974" "975" "976" "977" "978" "979" "980" 
[981] "981" "982" "983" "984" "985" "986" "987" "988" "989" "990" 
[991] "991" "992" "993" "994" "995" "996" "997" "998" "999" "1000" 

[[2]] 
[1] "time" "z1" "z2" "z3" "z4" "z5" "z6" "z7" "z8" "z9" 
[11] "z10" "z11" "z12" "z13" "z14" "z15" "z16" "z17" "z18" "z19" 
[21] "z20" 

> dim(foo) 
[1] 1000 21 

> names(foo) 
[1] "time" "1" "2" "3" "4" "5" "6" "7" "8" "9" 
[11] "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" 
[21] "20" 

尺寸看起來一樣嗎?這很奇怪,因爲原始框架上的matplot工作,但不在框架的頭部。這裏會發生什麼?

編輯,OK,所以回答一些問題,讓我們用thbyzone代替lhbyzone,因爲它的體積更小,讓我們用頭代替頭(..,1000),以使數據更小

> head(thbyzone) 
    time 1 2 3 4 5 6 7 8 9 10 11 
1 1 46 38 44 45 42 44 45 43 41 42 36 
2 2 46 36 42 43 42 43 44 44 39 43 32 
3 3 45 35 40 41 40 42 41 42 36 43 31 
4 4 41 30 36 37 39 38 40 34 35 39 30 
5 5 39 30 34 33 40 38 35 30 33 35 34 
6 6 35 29 32 32 41 37 35 35 36 35 35 

> dimnames(head(thbyzone)) 
[[1]] 
[1] "1" "2" "3" "4" "5" "6" 

[[2]] 
[1] "time" "1" "2" "3" "4" "5" "6" "7" "8" "9" 
[11] "10" "11" 

> matplot(head(thbyzone)) 
Error in `rownames<-`(`*tmp*`, value = c("1", "2", "3", "4", "5", "6", : 
    length of 'dimnames' [1] not equal to array extent 

> matplot(as.matrix(head(thbyzone))) 
Error in `rownames<-`(`*tmp*`, value = c("1", "2", "3", "4", "5", "6", : 
    length of 'dimnames' [1] not equal to array extent 

> matplot(thbyzone[1:6,]) 
Error in `rownames<-`(`*tmp*`, value = c("1", "2", "3", "4", "5", "6", : 
    length of 'dimnames' [1] not equal to array extent 

> class(thbyzone) 
[1] "cast_df" "data.frame" 

> str(thbyzone) 
List of 12 
$ time: num [1:39432] 1 2 3 4 5 6 7 8 9 10 ... 
$ 1 : int [1:39432] 46 46 45 41 39 35 33 33 36 47 ... 
$ 2 : int [1:39432] 38 36 35 30 30 29 28 28 28 33 ... 
$ 3 : int [1:39432] 44 42 40 36 34 32 30 30 30 32 ... 
$ 4 : int [1:39432] 45 43 41 37 33 32 30 29 30 41 ... 
$ 5 : int [1:39432] 42 42 40 39 40 41 38 33 36 43 ... 
$ 6 : int [1:39432] 44 43 42 38 38 37 36 36 38 44 ... 
$ 7 : int [1:39432] 45 44 41 40 35 35 33 30 31 39 ... 
$ 8 : int [1:39432] 43 44 42 34 30 35 34 33 34 41 ... 
$ 9 : int [1:39432] 41 39 36 35 33 36 32 31 31 35 ... 
$ 10 : int [1:39432] 42 43 43 39 35 35 33 33 35 42 ... 
$ 11 : int [1:39432] 36 32 31 30 34 35 32 30 28 30 ... 
- attr(*, "row.names")= int [1:39432] 1 2 3 4 5 6 7 8 9 10 ... 
- attr(*, "idvars")= chr "time" 
- attr(*, "rdimnames")=List of 2 
    ..$ :'data.frame': 39432 obs. of 1 variable: 
    .. ..$ time: num [1:39432] 1 2 3 4 5 6 7 8 9 10 ... 
    ..$ :'data.frame': 11 obs. of 1 variable: 
    .. ..$ station_id: int [1:11] 1 2 3 4 5 6 7 8 9 10 ... 

> traceback() 
.... lots of lines ... 
    "39405", "39406", "39407", "39408", "39409", "39410", "39411", 
    "39412", "39413", "39414", "39415", "39416", "39417", "39418", 
    "39419", "39420", "39421", "39422", "39423", "39424", "39425", 
    "39426", "39427", "39428", "39429", "39430", "39431", "39432" 
    )) 
4: as.matrix.cast_df(y) 
3: as.matrix(y) 
2: ncol(y <- as.matrix(y)) 
1: matplot(thbyzone[1:6, ]) 

一些更多的嘗試:

> foo <- head(thbyzone) 
> foo$time <- NULL 
> matplot(foo) 
Error in `rownames<-`(`*tmp*`, value = c("1", "2", "3", "4", "5", "6", : 
    length of 'dimnames' [1] not equal to array extent 
> head(foo) 
    1 2 3 4 5 6 7 8 9 10 11 
1 46 38 44 45 42 44 45 43 41 42 36 
2 46 36 42 43 42 43 44 44 39 43 32 
3 45 35 40 41 40 42 41 42 36 43 31 
4 41 30 36 37 39 38 40 34 35 39 30 
5 39 30 34 33 40 38 35 30 33 35 34 
6 35 29 32 32 41 37 35 35 36 35 35 

編輯:

> options(error=recover) 
> matplot(thbyzone[1:6,]) 
Error in `rownames<-`(`*tmp*`, value = c("1", "2", "3", "4", "5", "6", : 
    length of 'dimnames' [1] not equal to array extent 

Enter a frame number, or 0 to exit 

1: matplot(thbyzone[1:6, ]) 
2: ncol(y <- as.matrix(y)) 
3: as.matrix(y) 
4: as.matrix.cast_df(y) 
5: `rownames<-`(`*tmp*`, value = c("1", "2", "3", "4", "5", "6", "7", "8", "9" 
+0

那麼,沒有'lhbyzone'我不能重現錯誤,但你有沒有檢查它是否是由名稱引起的,例如通過做'名稱(lhbyzone)< - make.names(名稱(lhbyzone))'和'row.names(lhbyzone)< - NULL'? – Roland

+1

有些事情不對。昏暗(foo)不應該顯示1000行,如果它真的是頭(lhybyzone)。 'head'默認返回6行。這不是我們正在尋找的美孚。我們喜歡這裏的一致性,否則我們認爲「嘿,嘿,你沒有做你認爲你正在做的事情」。 – Spacedman

+0

你說得對,我正在使用'head(..,1000)'。編輯更新這 –

回答

5

OK,我可以從s重現此與reshape(但不包含reshape2)。有些東西確實被head()弄壞了。

d <- data.frame(time=rep(1:10,10),x=rep(1:10,each=10),y=1:100) 
library(reshape2) 
str(dcast(d,time~x)) ## regular data frame 
detach("package:reshape2") 
library(reshape) 
str(z <- cast(d,time~x)) 
matplot(head(z)) ## error 

的具體問題是utils::head.data.frame之間和as.matrix.cast_df的相互作用,它降低所述對象的片段,而不會保持完全一致的內部結構,(由matplot稱爲),其中假定結構是存在的。

添加下面的方法似乎解決了這個問題。

head.cast_df <- function (x, n = 6L, ...) { 
    stopifnot(length(n) == 1L) 
    n <- if (n < 0L) { 
     max(nrow(x) + n, 0L) 
    } else min(n, nrow(x)) 
    h <- x[seq_len(n), , drop = FALSE] 
    ## fix cast_df-specific row names element 
    attr(h,"rdimnames")[[1]] <- rdimnames(h)[[1]][seq_len(n),,drop=FALSE] 
    h 
} 

這可能是值得這個維護者接觸,雖然reshape包棄用,取而代之的reshape2(我認爲)...

另一種解決方法是,如果可以從reshape::cast切換到reshape2::dcast ...

+0

很酷。幹得好!我不知道如何在reshape2中使用融化(http://stackoverflow.com/questions/12990398/why-does-reshape2-melt-return-value-na-for-me)。我想我可以只卸載reshape2使用融化,然後加載它以便使用dcast? –

+0

Oooh,我可以使用'reshape :: melt'來使用重塑後的融化,並直接使用'dcast'從reshape2使用dcast。很酷,我會將這個答案標記爲已接受。謝謝你,本! –

+2

它不是頭,因爲'matplot(z [1:6,])'失敗相似... matplot變厚... – Spacedman

相關問題