[R] Apply functions along layers of a data matrix

2011-11-18 Thread saschaview
Hello How can I apply functions along layers of a data matrix? Example: daf - data.frame( 'id' = rep(1:5, 3), matrix(1:60, nrow=15, dimnames=list( NULL, paste('v', 1:4, sep='') )), rep = rep(1:3, each=5) ) The data frame daf contains 3 repetitions/layers (rep) of 4 variables of 5

Re: [R] Apply functions along layers of a data matrix

2011-11-18 Thread Dennis Murphy
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

Re: [R] Apply functions along layers of a data matrix

2011-11-18 Thread Paul Hiemstra
On 11/18/2011 01:05 PM, saschav...@gmail.com wrote: daf - data.frame( 'id' = rep(1:5, 3), matrix(1:60, nrow=15, dimnames=list( NULL, paste('v', 1:4, sep='') )), rep = rep(1:3, each=5) ) Hi, This seems like a job for plyr! library(plyr) ddply(daf, .(rep), summarise, mn = mean(v1))

Re: [R] Apply functions along layers of a data matrix

2011-11-18 Thread David Winsemius
On Nov 18, 2011, at 8:05 AM, saschav...@gmail.com wrote: Hello How can I apply functions along layers of a data matrix? Example: daf - data.frame( 'id' = rep(1:5, 3), matrix(1:60, nrow=15, dimnames=list( NULL, paste('v', 1:4, sep='') )), rep = rep(1:3, each=5) ) The data frame daf