Ryan Thompson Ryan Thompson -4 years ago 89
R Question

How can I easily get the mean, median ,quartiles, etc. given counts of each value in R?

Suppose I have a data frame with a column for values and another column for the number of times that value was observed:

x <- data.frame(value=c(1,2,3), count=c(4,2,1))
x
# value count
# 1 1 4
# 2 2 2
# 3 3 1


I know that I can get the weighted mean of the data using
weighted.mean
and the weighted median using the
weighted.median
function provided by several packages (e.g.
limma
), but how can I get other weighted statistics on my data, such as 1st and 3rd quartiles, and maybe standard deviation? "Expanding" the data using
rep
is not an option because
sum(x$count)
is about 3 billion (the size of the human genome).

Answer Source

Have you tried these packages:

  1. Hmisc -- it has several weighted statistics, including weighted quantiles

  2. laeken -- it has weighted quantiles.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download