I'm trying to find a better and faster way to put together a table of summary statistics comprised of weighted averages. Using dplyr to summarise and then bind_rows I end up with a table like this. These numbers are simple averages The averages are calculated for each factor for each group.
Dataframe: au.scores
AU AUDIT CORC GOV PPS TMSC TRAIN
1 Group1 2.833333 2.000000 2.733333 2.000000 1.750000 2.333333
2 Group2 2.833333 0.000000 2.733333 2.000000 1.750000 2.333333
3 Group3 1.833333 2.533333 2.466667 2.000000 2.500000 2.166667
4 Group4 3.000000 2.733333 2.200000 2.666667 1.583333 2.666667
5 Group5 2.625000 1.816667 2.533333 2.166667 1.895833 2.375000
Subsequent to this I need to derive a weighted score that combines the elements of each variable and groups 1 & 2 with 3, 4, 5. I.e., Overall.Group1 is Group1+Group4+Group5, Group2 is Group2+Group4+Group5 and Group3 is Group3+Group4+Group5 factors.
group1.overall <- data.frame(
group1.gov = (au.scores[3, 4] * .30) * .33 + (au.scores[1, 4] * .30) * .33 +
(au.scores[2, 4] * .30) * .33,
group1.corc = (au.scores[3, 3] * .30) * .33 + (au.scores[1, 3] * .1) * .33 +
(au.scores[2, 3] * .1) * .33,
group1.tmsc = (au.scores[3, 6] * .30) * .33 + (au.scores[1, 6] * .30) * .33 +
(au.scores[2, 6] * .30) * .33,
group1.audit = (au.scores[3, 2] * .30) * .33 + (au.scores[1, 2] * .30) * .33 +
(au.scores[2, 2] * .30) * .33,
group1.pps = (au.scores[3, 5] * .30) * .33 + (au.scores[1, 5] * .30) * .33 +
(au.scores[2, 5] * .30) * .33,
group1.train = (au.scores[3, 7] * .30) * .33 + (au.scores[1, 7] * .30) * .33 +
(au.scores[2, 7] * .30) * .33
)
Produces
group1.gov group1.corc group1.tmsc group1.audit group1.pps group1.train
1 0.7854 0.3168 0.594 0.7425 0.594 0.6765
Question Is there a quicker way to create the data.frame of overall scores?
Something like
Group_Num / Gov / Corc / Tmsc / Audit / PPS / Train / Overall
Group1 / 0.78 / 0.31 / 0.59 / 0.74 / 0.59 / 0.67 / <- sum these
Group2 / 0.66 / 0.23 / 0.44 / 0.66 / 0.22 / 0.43 / <- sum these
Group3 / 0.12 / 0.55 / 0.22 / 0.33 / 0.11 / 0.55 / <- sum these
etc
data.table
. It is pretty quick (much quicker than data.frame) – Duy Bui