[R] help eliminating for loops

Liaw, Andy andy_liaw at merck.com
Tue Jul 5 04:41:25 CEST 2005


Would this suffice:

n <- 10
NaCon <- matrix(rnorm(n * n, mean=10.77, sd=0.02), 10, 10)
MeanNaCon <- rowMeans(NaCon)
[...]

??

Andy

> From: John Sorkin
> 
> I am having trouble with apply. Could someone suggest changes to the
> code below that will eliminate the for loops?
> R 2.1.1 patched
> Windows 2k
> Thanks,
> John
>  
> function () 
> {
> NaCon<-array(dim=c(10,10))
> for (i in 1:10) {NaCon[i,]<-rnorm(10,10.77,0.02)}
> MeanNaCon<-vector(mode="numeric",length=10)
> for (j in 1:10) {MeanNaCon[j]<-mean(NaCon[j,1:10])}
> print(MeanNaCon)
> #assign("MakeNaCon",MakeNaCon)
> hist(MeanNaCon)
> }
>  
> John Sorkin M.D., Ph.D.
> Chief, Biostatistics and Informatics
> Baltimore VA Medical Center GRECC and
> University of Maryland School of Medicine Claude Pepper OAIC
>  
> University of Maryland School of Medicine
> Division of Gerontology
> Baltimore VA Medical Center
> 10 North Greene Street
> GRECC (BT/18/GR)
> Baltimore, MD 21201-1524
>  
> 410-605-7119 
> -- NOTE NEW EMAIL ADDRESS:
> jsorkin at grecc.umaryland.edu
> 
> 	[[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
> 
> 
>




More information about the R-help mailing list