19

我有一个 data.frame,我可以使用 matplot 绘制它:

> 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 有效,但不在框架的头部。这里会发生什么?

编辑,好的,所以回答一些问题,让我们使用 thbyzone 代替 lhbyzone,因为它更小,让我们使用 head 代替 head(..,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"
4

7 回答 7

12

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::castreshape2::dcast...

于 2012-10-20T15:39:15.770 回答
9

我有同样的错误。尽管此答案不适用于此问题的具体示例,但它可能会帮助未来访问此页面寻求错误帮助的用户。

我的列的名称包含数字和符号,例如17:901. 这是抛出错误。使用该make.names函数将我的列名转换为“语法上有效的名称”,例如。names(df)<-make.names(names(df))

这解决了问题。

于 2018-04-18T00:22:27.453 回答
3

我有同样的错误,但在我的情况下,一列(最后一列)在聚集后只有 NA 值。删除此列恢复了理智。您可以检查以确认您没有遇到类似的问题。

于 2019-01-22T10:55:58.430 回答
2

对于那些在使用时收到此错误的人mvoutlier::plot.mvoutlierCoDa(...),当参数onlyout(仅绘制异常值)设置为TRUE(默认值)并且您的数据集不包含任何异常值时会产生此错误。设置onlyout=FALSE应该工作。运行mvoutlier.CoDa(df)$outliers以查看您的数据集是否包含异常值。

于 2019-01-23T19:41:54.840 回答
0

可能有帮助 仅供参考

对于名称为股票代码的 xts 对象,我遇到了类似的问题。
对象 y 和 z

姓名(z)

[1] “000100 KS” “000333 C2” “000423 CH” “000651 CH” “000660 KS” “002008 C2” “002415 C2” “002456 C2” [9] “002475 CH”

"002747 频道"

all.equal(names(y),names(z)) TRUE

all.equal(index(y),index(z)) TRUE

暗淡(y) 489 10

暗淡(z) 489 10

昏暗(y[“2019-08”,])

[1] 31 10

昏暗(z[“2019-08”,])

[.xts(z, "2019-08", ) 中的错误:

'dimnames' [1] 的长度不等于数组范围

名称(z)<-make.names(名称(z))

昏暗(z[“2019-08”,])

[1] 31 10

不知道为什么 xts[] 选择适用于对象 y 而不是 z。

现在我只需要编写一个快速辅助函数来从新的“语法有效”(但不再是语义有效......)名称中获取代码。

于 2020-01-03T19:18:36.570 回答
0

我在使用 data.frame 时遇到了类似的问题,我试图将其转换为 data.table 并给了我同样的问题。我解决了:

data <- data %>% as.matrix(.) %>% as.data.table(.)
于 2021-03-24T05:35:33.930 回答
0

我收到错误是因为我使用了自定义列名,但不小心忘记了一列。在不选择列名的情况下尝试运行它以确保您没有遗漏任何内容可能值得尝试。

于 2021-09-20T06:25:13.893 回答