[R] function sum for array
William Dunlap
wdunlap at tibco.com
Fri Nov 18 17:11:27 CET 2011
S+'s help(apply) says
MARGIN
the subscripts over which the function is to be applied.
For example, if X is a matrix, MARGIN=1 indicates rows
and MARGIN=2 indicates columns. If the dimensions of X
are named, then the names can be used to specify MARGIN.
Note that MARGIN tells which dimensions of X are retained
in the result.
(and the Details section of the help file gives a worked
example) but I don't know if that is any clearer to you
than R's help(apply).
So if you want to summarize the one-dimensional slices,
X[i,j,k], of an I by J by K array X use MARGIN=c(1,2).
(If your summary has dimensions M by N by ... by P (or
just length M) then apply's output has dimensions
c(prod(M,N,...,P),K), except that any dimension equal to 1 is
dropped.)
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
From: Simone Salvadei [mailto:simone.salvadei at gmail.com]
Sent: Thursday, November 17, 2011 11:20 PM
To: William Dunlap
Subject: Re: [R] function sum for array
thank you very much, despite of the fact that I didn't understand how it really works. Why have you put 1:2?
S
On Thu, Nov 17, 2011 at 9:40 PM, William Dunlap <wdunlap at tibco.com> wrote:
The following gives a result identical to your 'B'.
> apply(A,1:2,sum)
[,1] [,2] [,3]
[1,] 532610 2130440 8521760
[2,] 1065220 4260880 17043520
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Simone Salvadei
> Sent: Thursday, November 17, 2011 8:25 AM
> To: r-help at r-project.org
> Subject: [R] function sum for array
>
> I'm looking for a function that allows to sum the elements of an array
> along a dimension that can be different from the classical ones (rows or
> columns).
>
> Let's suppose for example that:
>
> - A is an array with dimensions 2 x 3 x 4
> - I want to compute B, a 2 x 3 matrix with elements equal to the sum of the
> corrensponding elements on each of the 3 strata.
>
> I've tried to use apply(A,3,sum) but the result is a vector, not a matrix.
> Another solution is a less elegant
>
> B=matrix(rep(0,6),ncol=3)
> for(t in 1:4) B = B + A[ , , t]
>
> May anybody help?
> S
>
> --
> -------------------------------------------------------------------
>
> Simone Salvadei
>
> Faculty of Economics
> Department of Financial and Economic Studies and Quantitative Methods
> University of Rome Tor Vergata
> e-mail: simone.salvadei at uniroma2.it <federico.belotti at uniroma2.it>
> url: http://www.economia.uniroma2.it/phd/econometricsempiricaleconomics/
> <http://www.econometrics.it/>
> -------------------------------------------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.
--
-------------------------------------------------------------------
Simone Salvadei
Faculty of Economics
Department of Financial and Economic Studies and Quantitative Methods
University of Rome Tor Vergata
e-mail: simone.salvadei at uniroma2.it
url: http://www.economia.uniroma2.it/phd/econometricsempiricaleconomics/
More information about the R-help
mailing list