s-news
[Top] [All Lists]

[S] Summary: do.call("rbind", list)

To: S-news <s-news@wubios.wustl.edu>
Subject: [S] Summary: do.call("rbind", list)
From: "CIRAD EMVT" <lancelot@telecomplus.sn>
Date: Mon, 24 Jan 2000 08:51:59 +0000
Organization: Renaud Lancelot
References: <994DC7B72E4CD311A56D0008C707B359016D5375@usrymx05.merck.com>
Sender: owner-s-news@wubios.wustl.edu
Dear all,

Last week, I asked the following question:

> I have a list which is the result of by() on a dataframe. Each element
> of the list is a data frame with the same number of columns (13) and a
> variable number of lines (1 to 7). The length of the list is 1152. I
> want to turn this list into a data frame. I use do.call as follows, but
> it takes 25 minutes to complete !
>
> > length(Off2)
> [1] 1155
> > dim(Off2[[1]])
> [1]  7 13
> > dos.time(Off3 <- do.call("rbind", Off2))
> [1] 1499.66
>
> Is there a faster way to get the same result ?

I got 2 replies from Bert Gunter and Julian Taylor, using similar
methods:

Bert Gunter:
for(i in 1:length(Off2)) Off2[[i]] _ t(Off2[[i]])
matrix(unlist(Off2), ncol=13, byrow=T)

Julian Taylor:
t(matrix(unlist(sapply(Off2, function(el) t(el))), nrow =3)) 

Here is a small example:
> test <- list(A = data.frame(matrix(1:12, ncol = 3, byrow = T)), B = 
> data.frame(matrix(13:27, ncol
         = 3, byrow = T)))
> test2 <- do.call("rbind", test)
> test2
    X1.1 X1.2 X1.3 
A.1    1    2    3
A.2    4    5    6
A.3    7    8    9
A.4   10   11   12
B.1   13   14   15
B.2   16   17   18
B.3   19   20   21
B.4   22   23   24
B.5   25   26   27
> test2 <- test
> for(i in 1:length(test))
        test2[[i]] <- t(test[[i]])
> test2 <- matrix(unlist(test2), ncol = 3, byrow = T)
> test2
     [,1] [,2] [,3] 
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
[5,]   13   14   15
[6,]   16   17   18
[7,]   19   20   21
[8,]   22   23   24
[9,]   25   26   27
> test2 <- t(matrix(unlist(sapply(test, function(el)
t(el))), nrow = 3))
> test2
     [,1] [,2] [,3] 
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
[5,]   13   14   15
[6,]   16   17   18
[7,]   19   20   21
[8,]   22   23   24
[9,]   25   26   27

On my real data set, computing times are 1555.97, 85.59985 and 93.81006
for do.call, Bert's and Julian's solutions, respectively. The drawbacks
of Julian's and Bert's method are that all the columns are coerced to
character mode (I have both numerical and factor variables in my data
frame), and that column names are lost. However, this is easily managed
in a few subsequent steps.

Many thanks and best regards,

Renaud


-- 
Dr Renaud Lancelot, vétérinaire
CIRAD, Département Elevage et Médecine Vétérinaire (CIRAD-Emvt)
Programme Productions Animales
http://www.cirad.fr/presentation/programmes/prod-ani.shtml

ISRA-LNERV                      tel    (221) 832 49 02
BP 2057 Dakar-Hann              fax    (221) 821 18 79 (CIRAD)
Senegal                         e-mail renaud.lancelot@cirad.fr
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>
  • [S] Summary: do.call("rbind", list), CIRAD EMVT <=