[R] do.call("+", ...)
Robin Hankin
r.hankin at noc.soton.ac.uk
Fri Nov 17 15:15:02 CET 2006
Thanks for this Duncan
>>
>> Why is Peter Dalgaard's suggestion necessary? Why can't "+"
>> take more than two arguments?
>
> One reason is that it's a binary operator, so that's all it needs to
> take. We have the sum function for multiple operands.
>
> I would guess the historical reason is so that it can share code with
> other binary operators. For example, + currently shares code with the
> other binary operators -, *, /, ^, %%, %/%, but the grouping needed
> varies between them: a-b-c == (a-b)-c, but a^b^c == a^(b^c). R lets
> the parser handle operator binding.
>
OK, I see. But in algebra the "+" symbol is special: it is reserved
exclusively for associative and commutative operations [thus a+b+c is
always well-defined]; perhaps the parser could fall in with this
convention?
> By the way, another complaint is that sum() is supposed to be generic,
> but you can't define a sum.matrix() method so that sum(a,b,c) does the
> same as a+b+c when a is a matrix. (This would probably be a bad idea
> because people may be relying on the current behaviour, but R tries
> not
> to prevent people from testing out bad ideas.) ...
Can just clarify this? I can see that it's a bad idea, but I don't
quite
see why one *can't* do it. sum() is generic, and the manpage says
that methods can be defined for it directly.
OK, so I define a function sum.matrix()
[for the moment just taking two arguments]:
R> sum.matrix <- function(a,b){matrix(apply(matrix(c(a,b),ncol=2),
1,sum),dim(a))}
R> a <- matrix(1:6,2,3)
R> sum(a,a)
[1] 42
R> sum.matrix(a,a)
[,1] [,2] [,3]
[1,] 2 6 10
[2,] 4 8 12
R>
So why doesn't sum(a,a) dispatch to sum.matrix()?
best wishes
rksh
> You need to apply your
> own class to the matrices (e.g. mymatrix), then it all works:
>> f <- function(i){m <- matrix((1:6)^i,2,3); class(m) <-
>> "mymatrix"; m}
>>
>> sum.mymatrix <- function(...) {
> + x <- list(...)
> + return(x[[1]] + do.call(sum, x[-1]))
> + }
>>
>> do.call("sum",sapply(1:4,f,simplify=FALSE))
> [,1] [,2] [,3]
> [1,] 4 120 780
> [2,] 30 340 1554
> attr(,"class")
> [1] "mymatrix"
>
> Duncan Murdoch
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
tel 023-8059-7743
More information about the R-help
mailing list