I always thought I understood the concept of variance, but this one confuses me.

$\begin{eqnarray} X & := & (1, 2, 3)\\ E(X) & = & (1 + 2 + 3) / 3 = 2\\ Var(X) & = & Cov(X, X) = E((X - E(X))^2)\\ & = & E((-1, 0, 1)^2)\\ & = & E(1,0,1)\\ & = & 2/3 \end{eqnarray}$

But R tells me that

> var(c(1,2,3))
[1] 1


Which part of my calculation is wrong?

• You gave the formula for population variance. Sample variance is scaled by a factor of $\frac{n}{n-1}$, which adjusts for the fact that the sample is closer (in squared-deviation sense) to the sample mean than it is to the population mean; this fact imposes a downward bias on $n$-denominator sample variance estimates. As such, the unbiased version of sample variance is used very widely. see wikipedia. So R's answer will be 3/2 times as big as if you just apply the population-variance definition to a sample. Sep 1, 2013 at 23:59
• To clarify - your calculation isn't exactly wrong, since it's perfectly valid to use that for a sample variance -- but the usual convention is to apply the $n-1$ denominator for a sample variance. Sep 3, 2013 at 0:31

In the details of ?var we find:

The denominator n - 1 is used which gives an unbiased estimator of the (co)variance for i.i.d. observations.

so you should have $2/2=1$ instead. See here for some more details.