[R] within and between subject calculation
Douglas Bates
dmbates at gmail.com
Wed Jun 15 01:32:24 CEST 2005
On 6/14/05, Kerry Bush <kerryrekky at yahoo.com> wrote:
> Dear helpers in this forum,
>
> I have the following question:
>
> Suppose I have the following data set:
>
> id x y
> 023 1 2
> 023 2 5
> 023 4 6
> 023 5 7
> 412 2 5
> 412 3 4
> 412 4 6
> 412 7 9
> 220 5 7
> 220 4 8
> 220 9 8
> ......
>
> and i want to calculate sum_{i=1}^k
> sum_{j=1}^{n_i}x_{ij}*y_{ij}
>
> is there a simple way to do this within and between
> subject summation in R?
You didn't make it clear what the indices i and j refer to. It seems
that part of the answer could use
> samp
id x y
1 23 1 2
2 23 2 5
3 23 4 6
4 23 5 7
5 412 2 5
6 412 3 4
7 412 4 6
8 412 7 9
9 220 5 7
10 220 4 8
11 220 9 8
> with(samp, tapply(x*y, id, sum))
23 220 412
71 139 109
but if you then sum this result you simply get the sum of x * y.
More information about the R-help
mailing list