A few weeks ago I sent out a request about how to sum components of a vector
by group membership.
I had 139 groups, and a vector x, with N=17818 observations. I wanted the sum
of the components of x, for each group.
In "S-Plus" notation I wanted to define: sumx[i] <- sum(x[group==i]).
I received a request to send the most elegant solution to the entire list.
Here it is:
1. Sort the original datafile by group ahead of time.
2. Create a second data set, with one record per group and with two
variables. The first variable is the line that a given group starts on in the
main dataset, and the second is the line that it ends on. Call these
variables first and last.
3. Have a block of code in BUGS as follows:
for (i in 1:m){ #m = number of groups.
sumx[i] <- sum(x[first[i]:last[i]]);
}
Many thanks to those who responded.
Peter
Peter Austin PhD
Institute for Clinical Evaluative Sciences
Toronto, ON
Canada
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|