From berwin at maths.uwa.edu.au  Wed Nov 16 02:48:30 2005
From: berwin at maths.uwa.edu.au (Berwin A Turlach)
Date: Wed, 16 Nov 2005 09:48:30 +0800
Subject: [Rd] Bug or a feature that I completely missed?
Message-ID: <17274.36846.361077.416981@bossiaea.maths.uwa.edu.au>

Dear all,

while looking at some R-code submitted by students in a unit that I
teach, I came across constructs that I thought would lead to an error.
Much to my surprise, the code is actually executed.

A boiled down version of the code is the following:

> tt <- function(x, i){
+   mean(x[i,2])/mean(x[i,1])
+ }
> dat <- matrix(rnorm(200), ncol=2)
> mean(dat[,2])/mean(dat[,1])
[1] -1.163893
> dat1 <- data.frame(dat)
> tt(dat1)                 ###  Why does this work?
[1] -1.163893
> tt(dat)
Error in mean(x[i, 2]) : argument "i" is missing, with no default

Since the data for the assignment was in a data frame, the students got
an answer and not an error message when they called the equivalent of
tt(dat1) in their work.

I tested this code on R 1.8.1, 1.9.1, 2.0.0, 2.0.1, 2.1.0, 2.1.1,
2.2.0 and R-devel (2005-11-14 r36330), all with the same result, no
error message when executing tt(dat1).

I would have expected that tt(dat1) behaves in the same way as tt(dat)
and would produce an error.  Thus, I think it is a bug, but the fact
that so many R versions accept this code makes me wonder whether it is
a misunderstanding on my side.  Can somebody enlighten me why this
code is working?

Cheers,

        Berwin



[Rd] Bug or a feature that I completely missed?
Berwin A Turlach