#*** sum.grp ***
# from Chris Erdmann 6/7/96
# (see ~merrill/Mail/splus9606)

sum.grp _ function(X, Y)
{
#
#WHAT:  S function to sum across groups.  
#
#HOW IT WORKS:  Group membership is determined by values found in a vector X.
#	       Values in Y are summed for each group as determined by X.
#
#ASSUMPTION:  Groups are already grouped together.  If groups are not grouped
#             together, then need to add a line using "order" function.
#
#X = the vector with the id's
#Y = the vector with the values to be summed
#
        z4 <- match(unique(X), X)
        z5 <- z4 - 1
        z4.r <- z4
        z5.r <- z5[2:length(z5)]
        z5.r[length(z5)] <- length(Y)
        z6 <- cumsum(Y)
        z.len <- length(z4.r)
        z7 <- rep(NA, length = z.len + 1)
        z7[1] <- 0
        z7[2:length(z7)] <- z6[z5.r]
        z8 <- z7[2:length(z7)] - z7[1:length(z7) - 1]
        z8
}