How to apply functions across columns?
Indeed, apply is very flexible and idiomatically R (i.e., the right way to do it) -- but, just a heads up, for datasets with many rows/columns rowMeans and colMeans will be *much* faster (there's loop overhead in the apply family) Best, Michael
On Wed, May 9, 2012 at 3:46 PM, Robert Latest <boblatest at gmail.com> wrote:
On Wed, May 9, 2012 at 4:19 PM, R. Michael Weylandt <michael.weylandt at gmail.com> wrote:
Good reproducible example ;-) Easiest is probably just: cbind(tencor, ThisRowMean = ?rowMeans(tencor[, 4:8]))
Actually, after frying my brain on tapply() and sapply() I found that just plain apply() does what I need: tencor$mean <- apply(tencor[4:8], 1, FUN=mean) This way I'm also not tied to just mean() as aggregator but can use any homemade function (this would have been my followup question had I followed your advice ;-) Thanks! robert
______________________________________________ R-help at r-project.org 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.