[R] Does anyone.... worth a warning?!? No warning at all

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Mon Aug 20 23:11:46 CEST 2007

On 20-Aug-07 19:55:44, Rolf Turner wrote:
> On 20/08/2007, at 9:54 PM, Tom Willems wrote:
>> dear Mathew
>> mean is a Generic function
>> mean(x...)
>> in wich x is a data object, like a  data frame a list
>> a numeric vector...
>> so in your example it only reads the first character
>> and then reports it.
>> try x = c(1,1,2)
>> mean(x)
> I think you've completely missed the point. I'm sure Mathew
> now understands the syntax of the mean function. His point
> was that it would be very easy for someone to use this
> function incorrectly --- and he indicated very clearly *why*,
> by giving an example using max().
> If mean() could be made safer to use by incorporating a warning,  
> without unduly adding to overheads, then it would seem sensible
> to incorporate such a warning.  Or to change the mean()
> function so that mean(1,2,3) returns ``2'' --- just as max 
> (1,2,3) returns ``3'' --- as Mathew *initially* (and quite
> reasonably) expected it to do.
> cheers,
> Rolf Turner

I think Rolf makes a very important point. There are a lot of
idiosyncracies in R, which in time we get used to; but learning
about them is something of a "sociological" exercise, just as
one learns that when one's friend A says "X Y Z" is may not mean
the same as when one's friend B says it.

Another example is in the use of %*% for matrix multiplication
when one or both of the factors is a vector. If you came to R
from matlab/octave, where every vector is already either a row
vector or a column vector, you knew where you stood. But in R
the semantics of the syntax depend on the context in a more
complicated way. In R, x<-c(-1,1) is called a "vector", but it
does not have dimensions:


So its relationship to matrix multiplication is ambiguous.

For example:

M<-matrix(c(1,2,3,4),nrow=2); M
     [,1] [,2]
[1,]    1    3
[2,]    2    4

     [,1] [,2]
[1,]    1    1

and x is now coerced into a "column vector", which now (for that
immediate purpose) now does have dimensions (just as a row vector
would have in matlab/octave).


[1,]    2
[2,]    2

coerces it into a column vector. But now (asks the beginner who
has not yet got round to looking up ?"%*%") what happens with x%*%x?

Will we get column vector times row vector (a 2x2 matrix) or
row times column (a scalar)? In fact we get the latter:

[1,]    2

All this is in accordance with ?"%*%":

     Multiplies two matrices, if they are conformable. If one argument
     is a vector, it will be coerced to a either a row or column matrix
     to make the two arguments conformable. If both are vectors it will
     return the inner product.

But now suppose y<-c(1,2,3), with x<-c(-1,1) as before.

Error in x %*% y : non-conformable arguments

because it is trying to make the inner product of vectors of unequal
length. Whereas someone who had got as far as the second sentence of
the Description, and did not take the hird sentence as strictly
literally as intended, might expect that x would be coerced into
column, and y into row, so that they were conformable for
multiplication, giving a 2x3 matrix result (perhaps on the grounds
that "it will return the inner product" means that it will do this
if they are conformable, otherwise doing the coercions described
in the first sentence).

That misunderstanding could be avoided if the last sentence read:

"If both are vectors it will return the inner product provided
both are the same length; otherwise it is an error and nothing
is returned."

Or perhaps x or y should not be called "vector" -- in linear
algebra people are used to "vector" being another name for
a 1-dimensional matrix, being either "row" or "column".
The R entity is not that sort of thing at all. The closest
that "R Language Definition" comes to defining it is:

"Vectors can be thought of as contiguous cells containing
homogeneous data. Cells are accessed through indexing
operations such as x[5]."

x<-matrix(x) will, of course, turn x into a paid-up column vector
(as you might guess from ?matrix, if you copy the "byrow=FALSE"
from the "as.matrix" explanation to the "matrix" explanation;
though in fact that is irrelevant, since e.g. "byrow=TRUE" has
no effect in matrix() -- so in fact there is no specification
in ?matrix as to whether to expect a row or column result).

Just a few thoughts. As I say we all get used to this stuff in
the end, but it can be bewildering (and a trap) for beginners.

Best wishes to all,

E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 20-Aug-07                                       Time: 22:11:43
------------------------------ XFMail ------------------------------

More information about the R-help mailing list