# Computing SD for function of two RV's in R gives wrong results [duplicate]

I have 2 random variables, all independant, discrete and uniform

I want to compute standard deviation for Z = X - Y

Here is the R code:

#create X and Y
X = rep(0:1, c(733-41,41))
Y = rep(0:1, c(742-60,60))

#create Z as Y - X
Z = c()
for (i in X) {Z = c(Z,c(Y-i))}


Means are equal:

mean(Z)
[1] 0.02492802
mean(Y) - mean(X)
[1] 0.02492802


Then I count standard deviation and variance of Z using the formula:

sd(Y) + sd(X)
[1] 0.5027599
var(Y) + var(X)
[1] 0.1273021


Ann then, using sd() and var() functions:

sd(Z)
[1] 0.3565528
var(Z)
[1] 0.1271299


Where is my mistake and why sd(Y) + sd(X) != sd(Z) and var(Z) != var(Y) + var(X)?

Thanks!

• – whuber Apr 23 '16 at 1:44
• I don't see e defined anywhere. Is Z supposed to be c(X, Y)? That doesn't seem to be the way you created it. – gung - Reinstate Monica Apr 23 '16 at 1:45
• Sorry mate, e is Z actually, I fixed it. – Slava Chicago Apr 23 '16 at 11:05