Skip to content
Prev 303556 / 398503 Next

Apply a function according to factor levels.

Hello,

Or ?aggregate, depending on the wanted output. With the dataset provided 
by the op, apply function mean.

dat <- read.table(text="
  values ind sample
1  0.03325   1      1
2  0.03305   1      1
3  0.03185   1      1
[...etc...]
44 0.03060   5      4
45 0.06605   5      5
", header = TRUE)

tapply(dat$values, dat$sample, mean)  # named vector
        1        2        3        4        5
0.033556 0.011120 0.017750 0.030220 0.062060

aggregate(values ~ sample, data = dat, mean)  # data.frame
   sample   values
1      1 0.033556
2      2 0.011120
3      3 0.017750
4      4 0.030220
5      5 0.062060

Hope this helps,

Rui Barradas

Em 17-08-2012 02:41, David L Carlson escreveu: