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

John Kane jrkrideau at inbox.com
Wed Nov 11 14:04:59 CET 2015


I really don't understand what you are looking for but if S. Ellison's answer is not what you want what about this where your various x vectors are in a data frame

ibrary(reshape2)
library(plyr)

dat1  <-  structure(list(x1 = c(0.77, 0.22, 0.44), x2 = c(0.26, 0.89, 0.58
), x3 = c(0.2, 0.25, 0.91), x4 = c(0.06, 0.13, 0.26), x5 = c(0.65, 
0.16, 0.72)), .Names = c("x1", "x2", "x3", "x4", "x5"), row.names = c(NA, 
-3L), class = "data.frame")

m1  <-   melt(dat1)

ddply(m1, .(variable), summarize, variance = var(value))

John Kane
Kingston ON Canada


> -----Original Message-----
> From: marongiu.luigi at gmail.com
> Sent: Wed, 11 Nov 2015 11:26:25 +0000
> To: r-help at r-project.org
> Subject: [R] How to calculate variance on multiple numbers at once?
> 
> Dear all,
> 
> 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.
> 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?
> 
> 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?
> 
> Thank you
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!



More information about the R-help mailing list