0

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 do formula = 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
landroni
  • 2,902
  • 1
  • 32
  • 39
  • 1
    When you run `x.form <- formula(Species ~ .); x.form1 <- update(x.form, ~ . + Sepal.Width)`, what do you expect the result to be? Should `x.form` remain unchanged? – MrFlick Aug 07 '14 at 20:35
  • I too would like to know what you expect here. – Dason Aug 07 '14 at 20:36
  • 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 expect `update` to behave as usual. I included this in the OP. – landroni Aug 07 '14 at 20:37
  • 3
    But that's not how `.` normally works. Typically `.` means "all variables not otherwise specified". So if you did `terms.formula(Species ~ ., data=iris)` you would get `Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width`. Since you seem to want to re-define what `.` means, you are likely going to have to do some custom formula manipulation. – MrFlick Aug 07 '14 at 20:39
  • @MrFlick Hmm, I'm surprised. Shouldn't `update` handle cases with a `.`? But in any case, I need to update such formulas, so how can I detect when there is a `.` and whether it is LHS or RHS? – landroni Aug 07 '14 at 20:42
  • What is the motivation here? And what surprised you? It surprised me that you thought the natural thing to do when updating with `.` on the right side would be for the `.` to be dropped. – Dason Aug 07 '14 at 20:50
  • Well, it doesn't unfortunately, because it doesn't know names of the variables already represented by `.` so it doesn't know if what you are adding is already in the formula or not. So even if it did work, it would not likely work the way you want it to. This function seems to work for testing for a `.`: `hasdot<-function(f) {if(is.recursive(f)) {return(any(sapply(as.list(f), hasdot)))} else {f == as.symbol(".")}}` – MrFlick Aug 07 '14 at 20:56
  • 1
    @MrFlick The reason for my confusion is to be found in `?dcast`, whereas ""`.`" represents no variable" (see updated question). It's actually Hadley who seems to redefine what `.` means, and I'm trying to understand how to work around that.. – landroni Aug 07 '14 at 20:59
  • 2
    Then I understand why that's confusing. But the `update` function for formula is a base function and is unaware of Hadley's redefinition. Perhaps there is an alternate mechanism for updating formulas in the `reshape2` package. – MrFlick Aug 07 '14 at 21:01

0 Answers0