[R] R programming style

Roland Rau roland.rproject at gmail.com
Mon Feb 11 21:06:13 CET 2008


Hi,

Earl F. Glynn wrote:
> Instead of using "1" or "2" in an "apply", I'll write something like this 
> trying for some sort of mnemonic
> 
> apply(x, BY.ROW<-1, sum)
> or
> apply(z, BY.COL<-2, mean)
> 
It think it makes sense to use those "magic numbers" in the given case.
Please let me give you several arguments:

- In such a setting, I'd probably also use more mnemonic functions:
rowMeans
rowSums
colMeans
colSums

- The numbering of the MARGINs (the name of the second argument) is what 
I remember from maths: 1 is for rows, 2 index is for columns, ... So I 
don't think the numbering is counter-intuitive. For sure, you have to 
check the help page at least once. But this is also the case for using 
mnemonic arguments.

- The first argument in apply() is an array which is not restricted to 
two dimensions. For example, if you are working with three dimension, 
how would you specify it? BY.LAYER? Maybe, but then four dimensions or 
five dimensions?[1]

Please don't consider this as a personal criticism. I am sure that 
users' criticism improves R. But using mnemonics instead of the margins 
in the apply() case is not a convincing example, I think. Maybe you have 
another example?

Best,
Roland

[1] If you are curious whether there practical applications of four- or 
fivedimensional arrays, I can write to you off-list how useful they were 
in real world projects.



More information about the R-help mailing list