Barış Babahan
Barış Babahan

Reputation: 74

how to write R code that calculate correlation coefficient for 2 numbers

I have X = [2,3,5,10,8,5] Y = [7,6,9,1,6,3] variables I want to calculate correlation coefficient between them but without using "cor(X,Y)" function how can I achieve that? enter image description here

Upvotes: 0

Views: 520

Answers (1)

akrun
akrun

Reputation: 887501

If we want to use the formula, the Xbar is mean, sigma is sum and sqrt for the corresponding symbol in the formula

num1 <-  sum((X - mean(X))*(Y - mean(Y)))
den1 <- sqrt(sum((X - mean(X))^2) * sum((Y - mean(Y))^2))
num1/den1
[1] -0.599539

-testing with cor

cor(X, Y)
#[1] -0.599539

data

X <- c(2, 3, 5, 10, 8, 5)
Y <- c(7, 6, 9, 1, 6, 3)

Upvotes: 1

Related Questions