[R] How to speed up R code?

Duncan Murdoch murdoch at stats.uwo.ca
Tue Oct 18 14:39:23 CEST 2005


Martin Lam wrote:
> Hi,
> 
> I have written a piece of code, which is a variant of
> the random forest (rf) package algorithm, entirely in
> R. I know that some of the code in the rf package is
> written in c or c++. The problem is that the execution
> of my code in R takes a lot of time. To give you an
> example, the building and testing of data set with
> 20,000 instances using the random forest function from
> the rf package takes a few minutes while 'my' random
> forest's execution time is around 5 hours. So, I
> wonder if there are some ways to speed up the
> execution time. 
> 
> I've read in a similar post that using matrix instead
> of data.frame would actually speed up the R code. The
> format of my read-in data set is a "list", would the
> data set in matrix format (using as.matrix) be better?

One piece of advice I forgot to give to Xiaohua:  try it! You can 
probably write slow code using *either* matrices or lists.  You need to 
identify what is slow, and fix it.

Duncan Murdoch




More information about the R-help mailing list