s-news
[Top] [All Lists]

Re: rbind.Matrix (Matrix Library)

To: Pierre.Ilouga@evotecoai.com
Subject: Re: rbind.Matrix (Matrix Library)
From: Prof Brian Ripley <ripley@stats.ox.ac.uk>
Date: Thu, 26 Feb 2004 16:18:07 +0000 (GMT)
Cc: s-news@wubios.wustl.edu
In-reply-to: <OFF2AD270D.B1706978-ONC1256E46.004AC480@evotecoai.com>
On Thu, 26 Feb 2004 Pierre.Ilouga@evotecoai.com wrote:

> Dear Splus-users,
> 
> my question occurs due to the fact that the description of the function
> "rbind.Matrix" from the "Matrix" library is very poor. The goal is to avoid

It should not need any description: it is the rbind method for objects of
class Matrix, and it works by unpacking Matrix objects to matrices.  
Please do read the White Book about S3 generic functions and their
methods.  You shoul dnot be calling methods directly except in very
special circumstances.

> the function "rbind" and to use instead "rbind.Matrix" which is faster for
> very large data frames.

All you are doing is converting data frames to matrices, and it may be 
faster but at the expense of not doing the important part of the 
computations, matching up the column types in A and B.

> Let's consider the simplified example:
> 
> A<-data.frame(Fact=LETTERS[1:3],NR=1:3)         ## 3x3-data.frame
> B<-data.frame(Fact=LETTERS[1:2],NR=1:2)         ## 2x3-data.frame
> 
> C1<-rbind(A,B)
> > C1
>   Fact NR
> 1    A  1
> 2    B  2
> 3    C  3
> 4    D  1
> 5    E  2
> 
> library(Matrix)
> C2<-rbind.Matrix(A,B)
> > C2
>           Fact         NR
> [1,] factor, 3 numeric, 3
> [2,] factor, 2 numeric, 2
> attr(, "class"):
> [1] "Matrix"
> 
> Is there any Splus-function (or other possibilities except for loops) which
> enables to generate the data frame C1 from C2?

You don't want to do that!  It may well be faster to set up a data frame 
of the right size and type and assign blocks of rows.  E.g.

AB <- A[rep(1, nrow(A) + nrow(B)), ]
AB[1:nrow(A),] <- A
AB[nrow(A) + 1:nrow(B),] <- B

especially if this is being done for many data frames.

-- 
Brian D. Ripley,                  ripley@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


<Prev in Thread] Current Thread [Next in Thread>