在 R 编程中计算两个向量之间的协方差 - cov()函数
R 语言中的cov()
函数用于测量两个向量之间的协方差。
Syntax: cov(x, y, method)
Parameters:
x, y: Data vectors
method: Type of method to be used
示例 1:
# Data vectors
x <- c(1, 3, 5, 10)
y <- c(2, 4, 6, 20)
# Print covariance using Pearson method
print(cov(x, y, method = "pearson"))
输出:
[1] 30.66667
示例 2:
# Data vectors
x <- c(1, 3, 5, 10)
y <- c(2, 4, 6, 20)
# Print covariance using Pearson method
print(cov(x, y, method = "kendall"))
输出:
[1] 12
示例 3:
# Data vectors
x <- c(1, 3, 5, 10)
y <- c(2, 4, 6, 20)
# Print covariance using Pearson method
print(cov(x, y, method = "spearman"))
输出:
[1] 1.666667