R Variance


var() function calculates the variance of a vector.

var(x, y = NULL, na.rm = FALSE, use)
cov(x, y = NULL, use = "everything",
method = c("pearson", "kendall", "spearman"))
cov2cor(V)

x: numeric vector, matrix or data frame
y: numeric vector, matrix or data frame with compatiable dimensions to x, default is NULL
na.rm: missing values should be removed or not
use: method for computing covariances when missing values exist, including "everything", "all.obs", "complete.obs", "na.or.complete", "pairwise.complete.obs"
method: method for computing correlation coefficient, including "pearson", "kendall", "spearman"
V: symmeric numeric matrix

>BOD
Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8

> attach(BOD)
> var(demand)
[1] 21.44267

> head(iris,3)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
> cov(iris$Sepal.Length,iris$Petal.Length)
[1] 1.274315
> var(iris$Sepal.Length,iris$Petal.Length)
[1] 1.274315
> x <- iris[,c(1:4)]
> var(x)
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length    0.6856935  -0.0424340    1.2743154   0.5162707
Sepal.Width    -0.0424340   0.1899794   -0.3296564  -0.1216394
Petal.Length    1.2743154  -0.3296564    3.1162779   1.2956094
Petal.Width     0.5162707  -0.1216394    1.2956094   0.5810063








endmemo.com © 2025  | Terms of Use | Privacy | Home