[R] How to apply functions across columns?

Robert Latest boblatest at gmail.com
Wed May 9 21:46:34 CEST 2012


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



More information about the R-help mailing list