刚刚我花了一个小时去追踪我的代码中的'matrix vs data.frame'错误,我非常希望能够理解以下内容:
tmp <-
structure(c(4L, 7L, 5L, 12L, 6L, 11L, 9L, 3L, 1L, 2L, 10L, 8L),
.Dim = c(6L, 2L), .Dimnames = list(NULL, c("col1", "col2")))
## 1. This works:
plot(col2 ~ col1, data = tmp)
## 2. This doesn't work:
plot(col2 ~ col1, data = tmp, main = "hello")
## -> Error in FUN(X[[1L]], ...) : numeric 'envir' arg not of length one
## 3. This works:
plot(col2 ~ col1, data = as.data.frame(tmp), main = "hello")
在我看来,要么1和2都能工作,要么都会失败。1可以工作而2失败的事实让我在尝试让我的代码工作时走得很远。
我的问题是:为什么有时可以使用公式来绘制矩阵列,而不是总是或从不?当我添加一个标题到我的图表时会发生什么导致它失败?
编辑: 我怀疑我一定是自己弄坏了什么东西,所以我已经在多个新的R实例中尝试了这个确切的代码。我的sessionInfo是:
> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: i686-pc-linux-gnu (32-bit)
locale:
[1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_CA.UTF-8 LC_COLLATE=en_CA.UTF-8
[5] LC_MONETARY=C LC_MESSAGES=en_CA.UTF-8
[7] LC_PAPER=en_CA.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
>
编辑:
这是在失败的plot
命令后跟踪的结果:
> > traceback()
4: FUN(X[[1L]], ...)
3: lapply(dots, eval, data, parent.frame())
2: plot.formula(col2 ~ col1, data = tmp, main = "hello")
1: plot(col2 ~ col1, data = tmp, main = "hello")
>
编辑:
升级到2.14版本已解决该问题。
plot(col2 ~ col1, data = c(1,2), main = "hello")
或plot(col2 ~ col1, data = as.vector(tmp), main="hello")
获得您的错误。使用plot(col2 ~ col1, data = as.vector(tmp))
会导致类似但不同的错误,这是令人惊讶的。 - Brian DiggssessionInfo()
,干净运行的报告,traceback()
。 - IRTFM