Re: [R] within and between subject calculation

From: Douglas Bates <dmbates_at_gmail.com>
Date: Wed 15 Jun 2005 - 09:32:24 EST

On 6/14/05, Kerry Bush <kerryrekky@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.



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 15 09:35:33 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:38 EST