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.
A base solution for good measure.
by(df,df$group,function(x) sqrt(sum(x$vol^2)))
If you need it to look prettier:
as.table(df,df$group,function(x) sqrt(sum(x$vol^2))))
df$group
group1 group2 group3
0.04472136 0.14142136 0.12000000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With