[R] nicer matrix construction: rbind()

Gabor Grothendieck ggrothendieck at gmail.com
Wed Nov 30 16:17:12 CET 2005


On 11/30/05, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> On Wed, 30 Nov 2005, Martin Maechler wrote:
>
> > Just a small remark on "R coding style" :
> >
> >>>>>> "Sean" == Sean Davis <sdavis2 at mail.nih.gov>
> >>>>>>     on Tue, 29 Nov 2005 08:34:39 -0500 writes:
> >
> >      Sean> <............>
> >
> >      Sean> x <- matrix(c(1,1,1,2,2,2,1,1,1,1,1,2),nr=2,byrow=TRUE)
> >
> >      Sean>  <.....>
> >            (a very helpful answer to Georg's question; thanks, Sean!)
> >
> > I've seen example code like this in many places,
> > and I'd like advocate a more readable alternative
> >
> >   x <- rbind(c(1,1,1, 2,2,2)
> >              c(1,1,1, 1,1,2))
> >
> > {efficiency really not being of any concern;
> > we are talking about didactical examples}.
>
> But introducing unnecessary concepts is a concern, and I think
>
> x <- matrix(c(1, 1, 1, 2, 2, 2,
>               1, 1, 1, 1, 1, 2),
>             nrow = 2, byrow = TRUE)
>
> is clearer (and does not leave me wondering about the non-obvious choice
> of spaces).

This is harder to maintain since changing the matrix may also
require that one changes nrow thus introducing a potential source
of error not present in the rbind solution.




More information about the R-help mailing list