[Rd] confusing error from model.frame when var name=function name

Ben Bolker bbolker at gmail.com
Tue Nov 8 18:51:23 CET 2016


  This took me a few minutes of head-scratching:

Normally model.frame() gives an easily interpretable error if a variable
can't be found (in the data frame *or* elsewhere in the environment):

model.frame(~a,data=data.frame(x=1:5))
## Error in eval(predvars, data, env) : object 'a' not found

Now suppose you happen to have a variable name that matches an R
function name:

model.frame(~replicate,data=data.frame(x=1:5))
## Error in model.frame.default(~replicate, data = data.frame(x = 1:5)) :
##  object is not a matrix

   This happens somewhere inside a .External() call:

data <- .External2(C_modelframe, formula, rownames, variables,
    varnames, extras, extranames, subset, na.action)

so I haven't had the heart to track it all the way to its source yet.

  FWIW this happens whether the function is built-in or user-created.

  I don't think the possibly forthcoming "well just don't name your
variables that way" advice is entirely reasonable here ('replicate' is a
perfectly respectable variable name, as are many names that happen to
coincide with R function names (like 'c' !)

  I can easily implement my own checking function, at least for the
contents of the data frame (all(all.vars(formula) %in% names(data)):
checking for *non-function variables only* that exist anywhere in the
searchable environment is a bigger task), but this seems to me to be an
infelicity that would be lovely to have corrected ...

  I will post to R-bugs if this is not shot down in flames here.

  cheers
    Ben Bolker



More information about the R-devel mailing list