当我试图从Error: $ operator not defined for this S4 class运行ctree时,只有当公式被写为使用as.formula()转换的字符串时,才会得到as.formula()。
下面的例子是:
#This works fine :
y <- ctree(formula = quotation ~ minute + temp, data=test[[1]], controls = ctree_control(mincriterion = 0.99))
#While this doesn't :
x <- "ctree(formula = quotation ~ minute + temp, data=test[[1]], controls = ctree_control(mincriterion = 0.99))"
y <- as.formula(x)
Error: $ operator not defined for this S4 class我的最终目的是创建一个通过列表test来创建多个树的函数。
知道吗?
发布于 2015-09-10 14:05:34
ctree是一个函数,而不是公式。formula是由函数'~' (tilde)产生的对象的类。您可以从help('~')和help('formula')了解有关公式的更多信息。
使用as.formula最常用的方法是将表示公式语法的字符串转换为类公式的对象。有点像as.formula('y ~ x')。另外,检查class(as.formula(y~x))。
在您的示例中,您将表示函数ctree的字符串保存为变量x。函数ctree只包含表示公式语法(quotation ~ minute + temp)的字符串,但不能强迫它使用公式(它不代表公式,它只包含公式语法字符串),因为它不遵循公式语法。
如果您想要从文本中执行函数,则需要使用eval(parse(text = x)),尽管不鼓励这种技术。
https://stackoverflow.com/questions/32502507
复制相似问题