[R] curious about dimension of 'apply' output when MARGIN=1
Antonio, Fabio Di Narzo
antonio.fabio at gmail.com
Wed Jan 17 12:00:48 CET 2007
2007/1/17, Patrick Burns <pburns a pburns.seanet.com>:
> A logical reason for the phenomenon is that
> matrices are stored down their columns. For
> example:
>
> > matrix(1:15,5)
> [,1] [,2] [,3]
> [1,] 1 6 11
> [2,] 2 7 12
> [3,] 3 8 13
> [4,] 4 9 14
> [5,] 5 10 15
>
> When an 'apply' across rows is done, it will be
> the values corresponding to each of the rows that
> are together.
>
> For matrices, merely transposing the result fixes
> the "problem", but it is considerably more complex
> in higher dimensional arrays.
I routinely use 'aperm'
>
> There could be a spectrum of opinion from:
>
> the original programmer was lazy and didn't adequately
> serve users
>
> to:
>
> the simpler the program the fewer bugs there will be.
>
> Patrick Burns
> patrick a 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")
>
>
> Gabor Grothendieck wrote:
>
> >The reshape package has an idempotent apply, iapply:
> >
> >
> >
> >>library(reshape)
> >>iapply(M,1,function(row) row+c(1,2))
> >>
> >>
> > [,1] [,2]
> >[1,] 2 6
> >[2,] 3 7
> >[3,] 4 8
> >
> >On 1/16/07, Benjamin Tyner <btyner a stat.purdue.edu> wrote:
> >
> >
> >>Reading the documentation for 'apply', I understand the following is
> >>working exactly as documented:
> >>
> >> > M<-matrix(1:6,ncol=2)
> >> > M
> >> [,1] [,2]
> >>[1,] 1 4
> >>[2,] 2 5
> >>[3,] 3 6
> >> > apply(M,2,function(column) column+c(1,2,3))
> >> [,1] [,2]
> >>[1,] 2 5
> >>[2,] 4 7
> >>[3,] 6 9
> >> > apply(M,1,function(row) row+c(1,2))
> >> [,1] [,2] [,3]
> >>[1,] 2 3 4
> >>[2,] 6 7 8
> >>
> >>I'm not proposing any changes or extra arguments to 'apply'. Rather, I'm
> >>wondering what is the benefit for (or rationale behind) this somewhat
> >>unintuitive behavior in the case that MARGIN=1.
> >>
> >>Thanks,
> >>Ben
> >>
> >>______________________________________________
> >>R-help a 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.
> >>
> >>
> >>
> >
> >______________________________________________
> >R-help a 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.
> >
> >
> >
> >
>
> ______________________________________________
> R-help a 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.
>
--
Antonio, Fabio Di Narzo
Ph.D. student at
Department of Statistical Sciences
University of Bologna, Italy
More information about the R-help
mailing list