Summary table by group in R
dataframe, r
Solution
May I propose a solution using `ddply` function:
# require(plyr)
ddply(df, .(group), summarize, std = sqrt(sum(vol^2)))
# group std
# 1 group1 0.04472136
# 2 group2 0.14142136
# 3 group3 0.12000000
Problem
Consider the following dataframe: ``` df <- data.frame(group = c("group1", "group1", "group2", "group2", "group2", "group3"), factor = paste("factor", 1:6, sep=""), vol = seq(from = 0.02, length.out = 6, by = 0.02)) ``` The first column defines a top-level group for each factor in the second column. The third column is the value of standard deviation for each factor. I would like to generate a summary table with the groups only, and the standard deviation for each group defined as: If group1 contrains factors f1 and f2, and vol(f1) and vol(f2) are the standard deviations for factors f1 and f2 respectively, then the standard deviation of group1 is: `std(group1) = sqrt[vol(f1)^2 + vol(f2)^2]` Is there any easy way of creating the summary table, where `vol` of each group is computed using this custom function? Any help would be appreciated! Thank you.