is.data.frame(data)object ...在函数上下文中找不到 [英] is.data.frame(data) object ... not found in function context
本文介绍了is.data.frame(data)object ...在函数上下文中找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用包中的用户定义函数包 tree
cv.tree
函数有一个奇怪的问题:
func< - function(train){
classification.tree< - tree(log10(perf)〜syct + + mmax + cach + chmin + chmax,train,split =gini)
cv.tree(classification.tree,FUN = prune.tree,K = 4)
return(classification.tree)
}
数据(cpus,package =MASS)
result< - func(cpus)
plot(result)
这会产生错误:
.data.frame(data):没有找到对象'train'
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 ,8,9,10,11,12,13,14,
15,16,17 19,20,21,22,23,24,25,...
14 eval(expr,envir,enclosure)
13 eval expr,p)
12 eval.parent(m)
11树(公式= 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 ,24,25,...
10 eval(expr,envir,enclosure)
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)在.active-rstudio-document#9
4 eval(expr,envir,enclosure)
3 eval(ei,envir)
2 withVisible(eval(ei,envir))
1来源(〜/ .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)
我缺少什么?
解决方案
将这个特定的包需要数据集设为全局变量!这是怎么作为使用通用编程语言的论据?无论如何,下面的代码似乎起作用:
库(树)
pre>
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)
Have a strange problem using package
tree
cv.tree
function from user defined function in R: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)
This produces the error:
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)
At the same time if I invoke same code right from the script it works perfectly fine:
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)
What am I missing?
解决方案Turned out this particular package requires data set to be a global variable! How about this as an argument for using general purpose programming languages? Anyway code below seems to work:
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)
这篇关于is.data.frame(data)object ...在函数上下文中找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文