[R] How to calculate variance on multiple numbers at once?

S Ellison S.Ellison at LGCGroup.com
Wed Nov 11 13:58:13 CET 2015



> -----Original Message-----
> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Luigi
> Marongiu
> if I have a sample set of the following numbers x1=0.09, x2=0.94, x3=0.48,
> x4=0.74, x5=0.04 I can calculate the variance easily.
Not without concatenating them into a vector, you can't. You need them in a vector, as in
var( c(x1, x2, x3, x4, x5) )

> But if each x is actually a subset of multiple values, what would be the formula
> to calculate the variance? and it is possible to implement such mathematical
> function in R?
This is what R wants anyway, so the function you are looking for is var()

> For instance if I have the following: x1=(0.77, 0.22, 0.44), x2=(0.26, 0.89, 0.58),
> x3=(0.20, 0.25, 0.91), x4=(0.06, 0.13, 0.26) and x5=(0.65, 0.16, 0.72) how can i
> calculate the variance for each x?
var(x1)
var(x2)
....

or, if you want to be a bit more slick about it and do it in one line

lapply(list( x1, x2, x3, ...), var  ) 

(or sapply() if you want a vector result)






*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}



More information about the R-help mailing list