I have a csv file which has details about students and their weights from different schools
e.g.
School School code Weight Age Height
A 1 91 15 1.6
A 1 60 16 2.0
B 2 61 14 1.8
B 2 92 13 1.7
B 2 67 14 1.5
B 2 56 15 1.7
C 3 95 16 1.7
C 3 72 17 1.5
A 1 62 15 2.0
A 1 96 15 1.9
D 4 84 17 2.0
D 4 51 17 1.6
D 4 99 18 1.6
C 3 79 17 1.8
C 3 83 17 2.0
C 3 81 16 1.9
D 4 93 17 1.6
D 4 62 18 1.5
B 2 98 14 2.0
B 2 73 13 1.6
I would like to repeatedly sample n weights with replacement, sum my n weights and then work out the 95th quantile of the resulting distribution. I would like to do this for students within each of the 200 of the schools, varying n fro 1-25 to end up with the following output:
n=1 2 3 4 … 25
School code =1
2
3
4 95th percentile of distrinution
5
…
200
I am using tapply() to find the n=1 answer for all 200 schools
tapply(weight,schoolcode,quantile,probs=0.95)
and I am using replilcate(), sum() and sample() to simulate 1000 combinations of adding two weights together.
nstudents=replicate(1000, sum(sample(weight, size=n, replace=TRUE)
I am having trouble combining the two above so that to replicate sum and sample within the tapply function.
Please advise.
I am a beginner to R.