But do note -- again! -- that the apply family of functions do their magic **internally through looping**, so that they are generally not much faster -- and sometimes a bit slower -- then explicit loops. Their chief advantage (IMO, of course) is in code clarity and correctness, which is why I prefer them. (They are also written to do their looping as efficiently as possible, which explicit looping in user code may not.)
Of course, vectorized calculations (colMeans() in the example below) **are** much faster and usually clearer than explicit loops. Bert Gunter Genentech Nonclinical Statistics South San Francisco, CA 94404 -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Chuck Cleland Sent: Wednesday, December 06, 2006 6:54 AM To: R Help Subject: Re: [R] Usage of apply Jin Shusong wrote: > Dear R Users, > > Are there any documents on the usage of apply, tapply, > sapply so that I avoid explicit loops. I found that these > three functions were quite hard to be understood. Thank you > in advance. If you have read the help pages for each and possibly even consulted the reference on those help pages, you may need to elaborate on what parts of these functions you don't understand. You might also describe a loop you are contemplating and ask how it might be replaced by one of these functions. Here is a very simple example of a loop that could be avoided with one of these functions: > for(i in 1:4){print(mean(iris[,i]))} [1] 5.843333 [1] 3.057333 [1] 3.758 [1] 1.199333 Here is how you would do that with apply(): > apply(iris[,1:4], 2, mean) Sepal.Length Sepal.Width Petal.Length Petal.Width 5.843333 3.057333 3.758000 1.199333 Even better in this particular case would be: > colMeans(iris[,1:4]) Sepal.Length Sepal.Width Petal.Length Petal.Width 5.843333 3.057333 3.758000 1.199333 but you don't always want mean() or sum() as the function, so the functions you mention above are more general than colMeans() and similar functions. > > ------------------------------------------------------------------------ > > ______________________________________________ > 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 > 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 ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code. ______________________________________________ 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 and provide commented, minimal, self-contained, reproducible code.