Please use colMeans()! was: Re: [R] Calculate Mean of Column Vectors?
Mulholland, Tom
Tom.Mulholland at dpi.wa.gov.au
Tue Jan 11 09:51:46 CET 2005
There are indeed speed advantages in using colSums etc. However the disadvantage is that the newbie doesn't always find the power inherent in the apply, sapply, tapply and mapply. For many things that I do, the speed is the least of my worries; although I take the point that using apply for means or sums in packages that are distibuted to others is not the way to go.
As many of us have found out (and I think it was in S Poetry) the statement was made that vectorisation befuddles some beginners. So learning how to use this command, on a nice easy topic such as summing or averaging does have some merit. I have to admit using colSums a lot, but I don't think I have ever thought to use colMeans.
As an aside; does anyone have a list of optimised functions. That is functions like this one whose main benefit is speed (I think) I guess many of us feel that we are constantly using hammers to crack the nuts, but we still don't really know what's in the toolbox.
Tom
> -----Original Message-----
> From: Uwe Ligges [mailto:ligges at statistik.uni-dortmund.de]
> Sent: Tuesday, 11 January 2005 3:57 PM
> To: r-help at stat.math.ethz.ch
> Cc: 'Thomas Hopper'
> Subject: Please use colMeans()! was: Re: [R] Calculate Mean of Column
> Vectors?
>
>
> Folks, please see ?colMeans (as already pointed out in at least one
> message) and friends.
> These are faster than those apply(X, 2, mean) calls
> and please do use them when publishing code (both in packages
> in on this
> list).
>
> Uwe Ligges
>
>
> >>Hello,
> >>
> >>I've got an array defined as y <- rnorm(3000), dim(y) <- c(3, 1000).
> >>
> >>I'd like to produce a 1000-element vector z that is the mean of the
> >>corresponding elements of y (like z[1,1] <- mean(y[1,1], y[2,1],
> >>y[3,1])), but being new to R, I'm not sure how to do this for all
> >>elements at once (or, at least, simply). Any help is appreciated.
> >>
> >>Thanks,
> >>
> >>Tom
>
> ______________________________________________
> 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