r - 如何将公式作为字符串传递?
问题描述
我想将字符串作为aov
函数中的公式传递
这是我的代码
library(fpp)
formula <-
"score ~ single"
aov(
formula,
credit[c("single", "score")]
)
我的目标是输出与此相同
aov(score ~ single,
credit[c("single", "score")])
解决方案
这个问题似乎非常接近How to pass string formula to R's lm and see the formula in the summary? 除了那个问题涉及到lm
。
下面,do.call
确保formula(formula)
在发送到之前对其进行评估,aov
以便Call:
输出中的行正确显示;否则,它会从字面上显示formula(formula)
. do.call
不仅评估公式,还会评估credit
将其扩展为显示其所有值而不是单词的巨大输出,credit
因此我们quote
可以防止这种情况发生。如果您不关心该Call:
行的外观,则可以将其缩短为aov(formula(formula), credit)
.
do.call("aov", list(formula(formula), quote(credit)))
给予:
Call:
aov(formula = score ~ single, data = credit)
Terms:
single Residuals
Sum of Squares 834.84 95658.64
Deg. of Freedom 1 498
Residual standard error: 13.8595
Estimated effects may be unbalanced
推荐阅读
- html - 在 iPhone 上,具有“datepicker”类的 Input 元素不跨越整个列宽
- ios - 我们可以在 ios 中购买与礼品用户相同的产品代码吗?
- javascript - 如何为 before:: 和 after:: 之间的文本编写定位器
- r - R没有拿起用户库
- css - 为什么我的内联样式有效,但我的样式表根本没有更改代码?
- ansible - 如何为本地和远程主机设置不同的python解释器
- python-3.x - 从多级列索引中删除列
- flutter - 如何在弹出窗口中底部对齐按钮?
- module - YOCTO:图像中没有“/lib/modules”目录,modprobe 失败
- oauth-2.0 - FusionAuth - 基于邀请的用户登录与社交登录