Skip to content
Prev 277828 / 398506 Next

Apply functions along "layers" of a data matrix

Hi:

Here are two ways to do it; further solutions can be found in the doBy
and data.table packages, among others.

library('plyr')
ddply(daf, .(id), colwise(mean, c('v1', 'v2', 'v3', 'v4')))

aggregate(cbind(v1, v2, v3, v4) ~ id, data = daf, FUN = mean)

# Result of each:
  id v1 v2 v3 v4
1  1  6 21 36 51
2  2  7 22 37 52
3  3  8 23 38 53
4  4  9 24 39 54
5  5 10 25 40 55

Dennis
On Fri, Nov 18, 2011 at 5:05 AM, <saschaview at gmail.com> wrote: