Hi, I am new to programming with R but I have an assignment to manually code the correlation coefficient between two variables , X and Y, from a dataset. Both variables have 400 observations. I have been unsuccessful in properly computing the code thus far, this is what I have:

for(i in 1:n){

n = length(X)

Xbar = mean(X)

Ybar = mean(Y)

Xstat = X[i]-Xbar

Ystat = Y[i]-Ybar

r = (sum(Xstat * Ystat))/(sqrt((sum(Xstat^2))*(sum(Ystat^2))))

r

}

Any help would be great!