[R] error in dat

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Wed Feb 25 14:05:25 CET 2009

Prof Brian Ripley wrote:
> On Mon, 16 Jun 2008, Wacek Kusnierczyk wrote:
>> Paul Adams wrote:
>>> Hello everyone,
>>> I have the following code which keeps giving me an error.
>>> The code is:
>>> dat<-read.table(file="C:\\Documents and Settings\\Owner\\My
>>> Documents\\eisen.txt",header=T,row.names=1,blank.lines.skip=F,na..strings="NA")
>>> dimnames(dat)((1)) <-as.character(dat(,1))
>>> dat<-dat(,-1)
>>> dat<-as.data.frame(dat)
>>> file.show(file="C:\\Documents and Settings\\Owner\\My
>>> Documents\\eisen.txt")
>>> ann<-read.table(file="C:\\Documents and Settings\\Owner\\My
>>> Documents\\eisenClasses.txt",header=T)
>>> file.show(file="C:\\Documents and Settings\\Owner\\My
>>> Documents\\eisenClasses.txt")
>>> cl<-as.character(ann[,2])
>>> dat<-dat[,cl]
>>> gc<-cl(1:19)
>>> act<-cl(20:39)
>>> x<-as.numeric(dat(2000,gc))
>>> y<-as.numeric(dat(2000,act))
>>> x<-x(!is..na(x))
>>> y<-y(!is.na(y))
>>> xy.list<-list(x,y)
>>> boxplot(xy.list,col=c("red","blue"),main="Gene 2000")
>>> The error is:   "error in eval .with.vis(expr, envir, enclos)  :
>>> could not find function "dat"
>> you misuse the syntax, check the docs.  with 'dat(...)' r tries to apply
>> dat, but dat is a data frame, and is thus not applicable.  what you want
>> is dat[...].
>> you can argue that the error message is misleading;  unless you defined
>> one, r cannot find a function named 'dat', but it does find your data
>> frame, and it should complain about its non-applicability.
> It is this explanation which is misleading.  R (not r) looks for a
> function named 'dat': it does not find the data frame when looking for
> a function.  To be explicit, when R encounters foo() it looks in the
> current environment for a function named 'foo' and ignores all other
> objects named 'foo' even if they are higher on the search path.  This
> was not the behaviour of Blue-Book S, but it has been the behvaviour
> of S and R for many years.

are you sure it is not your explanation that is misleading?  as far as i
can see, bindings for function objects are made in environments in just
the same way as for other kinds of objects. 

when r looks for a function named 'foo', it checks, starting from the
current environment, for bindings for the name 'foo';  when it finds
one, it examines the corresponding value for its being a function
(roughly), and either successfully returns the value, or proceeds with
the enclosing environment.  this is what can be seen in the sources

   // starting at line 1223 in src/main/envir.c as of r48000
   while (rho != R_EmptyEnv) {
      vl = findVarInFrame3(rho, symbol, TRUE);
      if (vl != R_UnboundValue) {
         if (TYPEOF(vl) == CLOSXP || TYPEOF(vl) == BUILTINSXP ||
            return (vl);

clearly, appropriately named values *are* found and examined even if
they're not functions.  this is also acknowledged in the following comment:

   // starting at line 1208 in src/main/envir.c as of r48000

   Search for a function in an environment This is a specially modified
   version of findVar which ignores values its finds if they are not

note the 'finds'.

back to the original issue, your "it does not find the data frame when
looking for a function." is *wrong*.  r *does* find the data frame; r
ignores it because it is not a function, but it ignores it only *after*
the data frame has been *found* and *examined*.  it searches further,
finds no function named 'dat', and complains.

to a naive user, the error message discussed above may be misleading. 
if it said, e.g., that none of the objects named 'dat' is a function, it
might be easier for the user to find out what's wrong.


More information about the R-help mailing list