R:使用公式绘制矩阵列

时间:2011-11-28 23:14:51

标签: r

在我的代码中花了一个小时追逐'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():

> > 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解决了这个问题。

1 个答案:

答案 0 :(得分:6)

根据问题后面的评论主题,很明显 <{1}}中的 ,而R-2.12.1

R-2.14.0文件的R-2.13.0部分可能是指提及

时的错误修复
NEWS

注意:这个答案是基于Tyler的挖掘和上面的大量海报。我正在提升该线程的结论来回答状态,作为一种可能有用的提醒,告知这种错误 - * plot(<formula>, data=<matrix>,..) now works in more cases; similarly for points(), lines() and text(). 的每个版本中包含的修正。)