I use ddply quite a bit but I do not consider myself an expert. I have a data frame (df) with grouping variable "Group" which has values of "A", "B" and "C" and the variable to summarize, "Var" has numeric values. If I use
ddply(df, .(Group), summarize, mysum=sum(Var))
then I get the sum of each A, B and C, which is correct. But what I want to do is to sum over each grouping of the Group variables as they are arranged in the data frame. For instance, if the data frame has
Group Var
A 1.3
A 1.2
A 0.4
B 0.3
B 1.3
C 1.5
C 1.7
C 1.9
A 2.1
A 2.4
B 6.7
The Desired result
A 2.9
B 1.6
C 5.1
A 4.5
B 6.7
So, the desired output performs a mathematical function on each grouping of the Group variables, rather than on all instances of the individual Group variables. Can this be done in ddply?
Data
dat <- structure(list(Group = c("A", "A", "A", "B", "B", "C", "C", "C", "A", "A", "B"),
Var = c(1.3, 1.2, 0.4, 0.3, 1.3, 1.5, 1.7, 1.9, 2.1, 2.4, 6.7)),
.Names = c("Group", "Var"), class = "data.frame", row.names = c(NA, -11L))