I have the following dataframe:
set.seed(1)
y <- data.frame(a1 = rnorm(5) , b1 = rnorm(5), c1 = rnorm(5), a2 = rnorm(5), b2 = rnorm(5), c2 = rnorm(5))
I would like to obtain the correlations of the pairs of columns: cor(a1,a2), cor(b1,b2), cor(c1,c2)
I tried the following but NA's appear as output:
apply(y,2,function(x) cor(x[1],x[3]))
I would like to get the result equivalent to
cor(y[,1],y[,4])
cor(y[,2],y[,5])
cor(y[,3],y[,6])
In my actual data frame, I have many more pairs of columns.
Any ideas?
Thanks for your support.