Re: [R] nicer matrix construction: rbind()

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu 01 Dec 2005 - 02:17:12 EST

On 11/30/05, Prof Brian Ripley <ripley@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@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.



R-help@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 Received on Thu Dec 01 03:00:46 2005

This archive was generated by hypermail 2.1.8 : Thu 01 Dec 2005 - 04:31:32 EST