Hi,
Try this:
dat1<-read.table(text="
?no.?? values ind sample
1? 0.03325? 1????? 1
2? 0.03305? 1????? 1
3? 0.03185? 1????? 1
4? 0.03515? 1????? 1
-------------------------
-------------------------
42 0.01085? 5????? 2
43 0.01660? 5????? 3
44 0.03060? 5????? 4
45 0.06605? 5????? 5
",sep="",header=TRUE)
dat2<-within(dat1,{sample<-as.factor(sample)})
ddply(dat2,.(sample), function(x) mean(x$values))
# sample?????? V1
#1????? 1 0.033556
#2????? 2 0.011120
#3????? 3 0.017750
#4????? 4 0.030220
#5????? 5 0.062060
A.K.
----- Original Message -----
From: li li <hannah.hlx at gmail.com>
To: r-help <r-help at r-project.org>
Cc:
Sent: Thursday, August 16, 2012 9:31 PM
Subject: [R] Apply a function according to factor levels.
Dear all,
? I have a question on applying a function to the data according to factor
levels.
For example, for the data below, what is the best way to apply a function
to
"values" according to different levels of "samples" (1,2,3,4,5)?
? ? values ind sample
1? 0.03325? 1? ? ? 1
2? 0.03305? 1? ? ? 1
3? 0.03185? 1? ? ? 1
4? 0.03515? 1? ? ? 1
5? 0.03375? 1? ? ? 1
6? 0.01180? 1? ? ? 2
7? 0.01850? 1? ? ? 3
8? 0.02915? 1? ? ? 4
9? 0.06200? 1? ? ? 5
10 0.03230? 2? ? ? 1
11 0.03345? 2? ? ? 1
12 0.03385? 2? ? ? 1
13 0.03605? 2? ? ? 1
14 0.03225? 2? ? ? 1
15 0.01145? 2? ? ? 2
16 0.01805? 2? ? ? 3
17 0.02950? 2? ? ? 4
18 0.05995? 2? ? ? 5
19 0.03425? 3? ? ? 1
20 0.03575? 3? ? ? 1
21 0.03535? 3? ? ? 1
22 0.03380? 3? ? ? 1
23 0.03410? 3? ? ? 1
24 0.01050? 3? ? ? 2
25 0.01735? 3? ? ? 3
26 0.03140? 3? ? ? 4
27 0.06170? 3? ? ? 5
28 0.03430? 4? ? ? 1
29 0.03185? 4? ? ? 1
30 0.03165? 4? ? ? 1
31 0.03380? 4? ? ? 1
32 0.03235? 4? ? ? 1
33 0.01100? 4? ? ? 2
34 0.01825? 4? ? ? 3
35 0.03045? 4? ? ? 4
36 0.06060? 4? ? ? 5
37 0.03280? 5? ? ? 1
38 0.03350? 5? ? ? 1
39 0.03215? 5? ? ? 1
40 0.03545? 5? ? ? 1
41 0.03285? 5? ? ? 1
42 0.01085? 5? ? ? 2
43 0.01660? 5? ? ? 3
44 0.03060? 5? ? ? 4
45 0.06605? 5? ? ? 5
? Thank you.
? ? ? Hannah
??? [[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.