# [R] curious about dimension of 'apply' output when MARGIN=1

Robin Hankin r.hankin at noc.soton.ac.uk
Thu Jan 18 10:54:04 CET 2007

```On 18 Jan 2007, at 08:42, Prof Brian Ripley wrote:

> On Wed, 17 Jan 2007, Patrick Burns wrote:
>
>> A logical reason for the phenomenon is that
>> matrices are stored down their columns. For
>> example:
>
[snip]

> Or that the vision of the original designer was not limited to
> matrices.
> It just so happens that in this example the replacement is a single
> dimension the same size as the single margin used.  That's
> atypical, and
> normally the result dimension has no connection to the margin.  The
> design
> is to put the result dimension first, and the first item in the
> 'seealso'
> list is aperm().
>
> To my mind the only general solutions are to put the result dimension
> first or last.  I would have used last, but using first is slightly
> more
> efficient for the reason Pat gives.
>

The case Brian Ripley mentions above is indeed atypical,
but it's encountered reasonably often, at least in the world of
high-dimensional magic hypercubes.

The most prominent examples would be sort()  and the identity function.

Although the emphasis is different,
matlab's  "sort" function when called with  two
arguments,  as in "sort(a,i)", sorts along  dimension "i",
leaving the dimensions of the returned array unchanged.

R-and-octave.txt includes the following:

a <- array(1:24,2:4)

....

%Sort is a bit of a problem, due to the behaviour of apply():

sort(a,1)           aperm(apply(a,c(2,3),sort),c(1,2,3))
sort(a,2)           aperm(apply(a,c(1,3),sort),c(2,1,3))
sort(a,3)           aperm(apply(a,c(1,2),sort),c(2,3,1))

It's possible to get round this by defining a little function:
asort <- function(a,i){
j <- 1:length(dim(a))
aperm(apply(a,j[-i],sort),append(j[-1],1,i-1))
}

Then R's asort(a,1) will return the same as Octave's sort(a,1).

Function asort() could easily be adapted to take a function name as a
third argument.

> apply() is for arrays, operating over one or more margins with a
> function
> returning a 'scalar', vector or array result.  Perhaps any 'lazy'-
> ness /
> limit of vision is not in handling array results as well as might be
> possible.
>
>> Patrick Burns
>> patrick at burns-stat.com
>> +44 (0)20 8525 0696
>> http://www.burns-stat.com
>> (home of S Poetry and "A Guide for the Unwilling S User")
>
>
> --
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> 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

```