# [R] Efficient marginal sums?

(Ted Harding) ted.harding at wlandres.net
Sat Nov 13 16:49:26 CET 2010

```Hi Folks,
[This is not unrelated to the current "vector of vectors" thread,
but arises quite independently]

Say I have a function f(x,y) which computes a value for scalar
x and y; or, if either x=X or y=Y is a vector, a corresponding
vector of values f(X,y) or f(x,Y) (with the usual built-in
vectorisation of operations).

Now I have X=(x.1,x.2,...,x.m) and Y=(y.1,y.2,...,y.n).
I'm seeking a fast and efficient method to compute (say)

sum[over elements of Y](f(X,Y))

returning an m-vector in which, for each x.i in X, I have

sum(f(x.i,Y))

I know I can do this by constructing matrices say M.X and M.Y

M.X <- matrix(rep(X,length(Y)),nrow=length(Y),byrow=TRUE)
M.Y <- matrix(rep(Y,length(X)),nrow=length(Y))

then F <- f(M.X,M.Y), then colSums(F). But that doesn't strike me
as particularly "fast and efficient", because of the preliminary
spadework to make M.X and M.Y.

Is there any much better way? Or some function somewhere that
does it? Something like "marg.sum(X,Y,function=f,margin=2)";
therefore with scope for generalisation to more than 2 variables,
e.g. marg.sum(X,Y,Z,function=f,margins=2)
or   marg.sum(X,Y,Z,function=f,margins=c(2,3))

Or, even more general, like:

marg.apply(X,Y,Z,fun1=f,fun2=sum,margins=c(2,3))

(Such a question must have been asked before, but I haven't
located it).

With thanks,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 13-Nov-10                                       Time: 15:49:22
------------------------------ XFMail ------------------------------

```