I am using the formula conventions in ?dcast
, namely the use of a .
(dot), and I've run into trouble when having to update the formula.
Consider this:
require(reshape2)
x.form <- formula(Species ~ .)
dcast(iris, x.form)
From ?dcast
:
There are a couple of special variables: "
...
" represents all other variables not used in the formula and ".
" represents no variable, so you can doformula = var1 ~ .
.
Now if I try to add a RHS term I get an error:
> x.form1 <- update(x.form, ~ . + Sepal.Width)
Error in terms.formula(tmp, simplify = TRUE) :
'.' in formula and no 'data' argument
I can work around this if I avoid the .
when updating:
> (x.form1 <- update(x.form, ~ + Sepal.Width))
Species ~ Sepal.Width
However this replaces the RHS: If I don't know beforehand that the RHS contains only a .
, then I butcher the formula instead of amending it.
So what is the best strategy in this case to always ensure that the formula is correctly updated? If there is a .
, I would expect it to be replaced by the added variable (i.e. Species ~ Sepal.Width
). If there is no .
, then I would expect update
to behave as usual.
PS When the .
is in the LHS, the behavior is still inconsistent:
x.form <- formula(. ~ Species)
dcast(iris, x.form)
And now there is no error but a rather strange result:
> (x.form1 <- update(x.form, . + Sepal.Width ~ .))
. + Sepal.Width ~ Species