Luigi Marongiu
2015-Nov-11 11:26 UTC
[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
S Ellison
2015-Nov-11 12:58 UTC
[R] How to calculate variance on multiple numbers at once?
> -----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}}
John Kane
2015-Nov-11 13:04 UTC
[R] How to calculate variance on multiple numbers at once?
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!