在 R 编程中计算两个向量之间的相关系数值 - cor()函数
R语言中的cor()
函数用于测量两个向量之间的相关系数值。
Syntax: cor(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(cor(x, y, method = "pearson"))
输出:
[1] 0.9724702
示例 2:
# Data vectors
x <- c(1, 3, 5, 10)
y <- c(2, 4, 6, 20)
# Print covariance using Pearson method
print(cor(x, y, method = "kendall"))
输出:
[1] 1
示例 3:
# Data vectors
x <- c(1, 3, 5, 10)
y <- c(2, 4, 6, 20)
# Print covariance using Pearson method
print(cor(x, y, method = "spearman"))
输出:
[1] 1