Re: [R] Faster way of binding multiple rows of data than rbind?

From: Douglas Bates <bates_at_stat.wisc.edu>
Date: Tue 08 Mar 2005 - 04:28:26 EST

Ken Termiso wrote:
> Hi all,
>
> I have a vector that contains the row numbers of data taken from several
> filtering operations performed on a large data frame (20,000rows x
> 500cols).
>
> In order to output this subset of data, I've been looping through the
> vector containing the row numbers (keepRows).
>
> output <- data.frame(row.names = rownames(bigMatrix))
>
> for(i in keepRows)
> {
> output <- rbind(output, bigMatrix[i, ])
> }
>
>
> As you may guess, doing all of these rbinds takes a LOT of time, so I'm
> wondering if there's a workaround where I can maybe use an intermediate
> matrix-like object to store the loop output, and then coerce it back to
> a data frame after the loop is complete??

The indexing operations in R are very flexible. You can do this in a single operation as

output <- bigMatrix[keepRows, ]



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 Tue Mar 08 04:33:54 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:41 EST