On 12-Mar-04 Padmanabhan, Sudharsha wrote:> I have an array c as follows
>
> 1 23 12
> 1 34 15
> 1 45 67
> 1 45 87
> 2 78 23
> 2 65 19
> 2 45 90
> 2 70 32
>
>
> Col 1 indicates treatment, col2 is the observed readings and col3
> is some summary statistic I have calculated after breaking the ties
> in col 2 arbitrarily.
>
> Now I want to average the statistics of the tied values. So, in col 3
> for each of the reading of 45 , I now want (67+87+90)/3 = 81.33
>
>
> 1 23 12
> 1 34 15
> 1 45 81.33
> 1 45 81.33
> 2 78 23
> 2 65 19
> 2 45 81.33
> 2 70 32
tmp<-matrix(c(
1,23,12,
1,34,15,
1,45,67,
1,45,87,
2,78,23,
2,65,19,
2,45,90,
2,70,32),ncol=3,byrow=TRUE)
for( i in (unique(tmp[,2])) ){
m <- mean(tmp[tmp[,2]==i,3]);
tmp[tmp[,2]==i,3] <- m
}
tmp
[,1] [,2] [,3]
[1,] 1 23 12.00000
[2,] 1 34 15.00000
[3,] 1 45 81.33333
[4,] 1 45 81.33333
[5,] 2 78 23.00000
[6,] 2 65 19.00000
[7,] 2 45 81.33333
[8,] 2 70 32.00000
(You could make the assignment within the for-loop even more
concise but the above shows explicitly what is going on).
Hope this helps!
Ted.
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 167 1972
Date: 12-Mar-04 Time: 22:52:24
------------------------------ XFMail ------------------------------