我在?dcast
中使用公式约定,即使用.
(点),并且在更新公式时遇到了麻烦。
考虑一下:
require(reshape2)
x.form <- formula(Species ~ .)
dcast(iris, x.form)
来自?dcast
:
有几个特殊变量:“
...
”表示公式中未使用的所有其他变量,“.
”表示无变量,因此您可以执行formula = var1 ~ .
。< / p>
现在,如果我尝试添加RHS术语,我会收到错误:
> x.form1 <- update(x.form, ~ . + Sepal.Width)
Error in terms.formula(tmp, simplify = TRUE) :
'.' in formula and no 'data' argument
如果我在更新时避开.
,我可以解决这个问题:
> (x.form1 <- update(x.form, ~ + Sepal.Width))
Species ~ Sepal.Width
然而,这个取代 RHS:如果我事先不知道RHS只包含.
,那么我会对公式进行屠宰而不是修改它。
那么在这种情况下,始终确保公式正确更新的最佳策略是什么?如果有.
,我希望它会被添加的变量替换(即Species ~ Sepal.Width
)。如果没有.
,那么我希望update
能够像往常一样行事。
PS当.
在LHS中时,行为仍然不一致:
x.form <- formula(. ~ Species)
dcast(iris, x.form)
现在没有错误,只有一个相当奇怪的结果:
> (x.form1 <- update(x.form, . + Sepal.Width ~ .))
. + Sepal.Width ~ Species