is.data.frame(data)object ...在函数上下文中找不到

时间:2015-01-26 10:38:31

标签: r function dataframe

使用来自R中用户定义函数的包tree cv.tree函数有一个奇怪的问题:

func <- function(train) {
    classification.tree <- tree(log10(perf) ~ syct+mmin+mmax+cach+chmin+chmax, train, split = "gini")
    cv.tree(classification.tree, ,FUN=prune.tree, K = 4)
    return (classification.tree)
}
data(cpus, package="MASS")
result <- func(cpus)
plot(result)

这会产生错误:

Error in is.data.frame(data) : object 'train' not found 
16 is.data.frame(data) 
15 model.frame.default(formula = log10(perf) ~ syct + mmin + mmax + 
    cach + chmin + chmax, data = train, subset = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",  ... 
14 eval(expr, envir, enclos) 
13 eval(expr, p) 
12 eval.parent(m) 
11 tree(formula = log10(perf) ~ syct + mmin + mmax + cach + chmin + 
    chmax, data = train, split = "gini", subset = c("1", "2", 
"3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", 
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25",  ... 
10 eval(expr, envir, enclos) 
9 eval(oc) 
8 model.frame.tree(object) 
7 model.frame(object) 
6 cv.tree(classification.tree, , FUN = prune.tree, K = 4) at .active-rstudio-document#4
5 func(cpus) at .active-rstudio-document#9
4 eval(expr, envir, enclos) 
3 eval(ei, envir) 
2 withVisible(eval(ei, envir)) 
1 source("~/.active-rstudio-document", echo = TRUE) 

同时,如果我从脚本中直接调用相同的代码,它的工作原理非常好:

data(cpus, package="MASS")
classification.tree <- tree(log10(perf) ~ syct+mmin+mmax+cach+chmin+chmax, cpus, split = "gini")
cv.tree(classification.tree, ,FUN=prune.tree, K = 4)
plot(classification.tree)

我错过了什么?

2 个答案:

答案 0 :(得分:1)

崩溃发生在cv.tree()电话中。 更新:cv.tree调用model.frame,在该函数内部有eval,但变量train在该函数的环境中不存在。

我会继续挖掘....如果我深入调试model.frame的调试模式,并更改&#39;对象的data列表元素&#39;来自&#39; train&#39;到&#39; cpus&#39;,然后eval找到对象并执行。

Annnnd:我回到了我开始的地方。这是一个环境和懒惰的评估问题 修复方法是使用force

func <- function(train) {
    force(train)
    classification.tree <- tree(log10(perf) ~ syct+mmin+mmax+cach+chmin+chmax, train, split = "gini")
    cv.tree(classification.tree, FUN=prune.tree, K = 4)
    return (classification.tree)
}

这使得&#34;训练&#34;存在于cv.tree可用的环境及其调用的函数中。环境变得奇怪:-);这是其中的一个例子。

答案 1 :(得分:0)

原来这个特殊的包需要将数据集作为全局变量!作为使用通用编程语言的论据,这怎么样?无论如何,下面的代码似乎有效:

library(tree)

train_global <- NA
func <- function(train) {
  train_global <<- train
  t <- tree(formula=log10(perf) ~ syct+mmin+mmax+cach+chmin+chmax, data=train_global, split = "gini")    
  cv.tree(t, ,FUN=prune.tree, K = 4)    
  return (t)    
}    
data(cpus, package="MASS")    
cpus <- cpus    
result <- func(cpus)
plot(result)