[R] nicer matrix construction: rbind()

Martin Maechler maechler at stat.math.ethz.ch
Wed Nov 30 10:27:35 CET 2005


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}.

I believe  rbind() -- and indenting ("white space"
in general!) should be used much more for matrix construction --
because of quick "visual validation"
at least for small examples.

{and if you'd use ESS (http://ESS.r-project.org/), 
 a simple <Tab> key press automatically indents the 2nd line correctly!}

Martin Maechler, ETH Zurich




More information about the R-help mailing list