Re: [R] help eliminating for loops

From: Liaw, Andy <andy_liaw_at_merck.com>
Date: Tue 05 Jul 2005 - 12:41:25 EST


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@grecc.umaryland.edu
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
>
>
>



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 Jul 05 12:48:06 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:12 EST