[R] Too slow to execute!

Chuck Cleland ccleland at optonline.net
Sun Apr 29 16:30:36 CEST 2007


Usman Shehu wrote:
> Greetings,
> I have the following simple function but what worries me is that it takes about  5 or more minutes to execute. My machine runs on windows with 1.8GHz and 256 Ram.
>> Re=NULL
>> for(i in 1:100000){
> + x=rnorm(20)
> + Re[i]=(x-2*10)/20
> + Re
> + }
> I would appreciate any help on how to make it faster.

  Why use a loop here at all?  It seems like this would do it pretty fast:

Re <- rnorm(n = 2e+06, mean = - 1)

> system.time(Re <- rnorm(n = 2e+06, mean = - 1))
[1] 0.77 0.01 0.78   NA   NA

  I'm not sure what you expect Re to be (a list with 100000 vectors of
length 20?), but you could reshape one long vector into a matrix or
whatever you might need.  For example:

Re.mat <- matrix(Re, ncol=20)

> Usman
> 
> 
> 
> 
>       ___________________________________________________________
> 
> now.
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
> 


-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list