[R] average of y at each level of x?
kjetil@entelnet.bo
kjetil at entelnet.bo
Sat Nov 8 04:56:35 CET 2003
On 7 Nov 2003 at 9:47, Bill Simpson wrote:
Since your y is 0-1 the following works:
tapply(x, y ,mean)
aggregate(x, y, mean)
gives the output in a different format.
Kjetil Halvorsen
> I have x,y data and would like to compute the average of y at each level
> of x.
>
> > x
> [1] 0.006110 0.007027 0.007027 0.007027 0.008081 0.008081 0.008081 0.008081
> [9] 0.008081 0.008081 0.008081 0.009293 0.009293 0.009293 0.009293 0.009293
> [17] 0.009293 0.009293 0.009293 0.010686 0.010686 0.010686 0.010686 0.010686
> [25] 0.010686 0.010686 0.010686 0.010686 0.010686 0.012289 0.012289 0.012289
> [33] 0.012289 0.012289 0.012289 0.012289 0.012289 0.012289 0.012289 0.012289
> [41] 0.012289 0.012289 0.014133 0.014133 0.014133 0.014133 0.016253 0.018691
> [49] 0.021494 0.024718 0.028426 0.032690 0.037594 0.043233 0.049718 0.057175
> [57] 0.065752 0.075614 0.086957 0.100000
>
> > y
> [1] 0 0 1 0 0 1 0 1 1 0 1 1 1 0 1 0 1 0 1 0 1 1 0 1 1 0 1 0 1 1 1 0 1 1 1 1 1 1
> [39] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>
> > table(x,y)
> y
> x 0 1
> 0.00611 1 0
> 0.007027 2 1
> 0.008081 3 4
> 0.009293 3 5
> 0.010686 4 6
> 0.012289 1 12
> 0.014133 0 4
> 0.016253 0 1
> 0.018691 0 1
> 0.021494 0 1
> 0.024718 0 1
> 0.028426 0 1
> 0.03269 0 1
> 0.037594 0 1
> 0.043233 0 1
> 0.049718 0 1
> 0.057175 0 1
> 0.065752 0 1
> 0.075614 0 1
> 0.086957 0 1
> 0.1 0 1
>
> I would like to compute p = count1/(count0 +count1). For example, for
> x=0.012289, p= 12/(1+12)= 0.923077. Please tell me how to do it. Maybe
> there is a better way to do this without going through table().
>
> Thanks very much for any help.
>
> Bill Simpson
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
More information about the R-help
mailing list