How can I, in R calculate the overall variance and the variance for each group from a dataset that looks like this (for example):
Group Count Value
A 3 5
A 2 8
B 1 11
B 3 15
I know to calculate the variance as a whole, ignoring the groups I would do:
var(rep(x$Value, x$Count)),
but how do I automatically calculate the variance for each group accounting for the frequency? E.g., the variance for group A, group B, etc.,.. I would like my output to have the following headers:
Group, Total Count, Group Variance
I have also reviewed this link; R computing mean, median, variance from file with frequency distribution which is different (does not have the group component) so this is not a duplicate.
thank you for all of the help.
One option is using
data.table
. Convert the data.frame to data.table (setDT
) and get thevar
of "Value" andsum
of "Count" by "Group".a similar way using
dplyr
isHere's a quick wrapper with base R. First step is to grow your data set by Count, and then calculate variance by group
Or similarly