[R] Apply functions along "layers" of a data matrix
saschaview at gmail.com
saschaview at gmail.com
Fri Nov 18 14:05:24 CET 2011
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 persons (id). For some reason, I want to calculate various
statistics (e.g., mean, median) *along* the repetitions. The "mean"
calculation, for example, would produce the means of daf[1, 'v1']
*along* the 3 repetition:
(daf[1, 'v1'] + daf[6, 'v1'] + daf[11, 'v1']) / 3
That is to say, each of the calculations would result in a data frame
with 4 variables (and the id) of the 5 persons:
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
Currently, I do this in a loop, but I was wondering about a quick and
ressource-friendly way to achieve this?
Thanks
*S*
--
Sascha Vieweg, saschaview at gmail.com
More information about the R-help
mailing list