I have the following data frame:
y <- data.frame(group = letters[1:5], a = rnorm(5) , b = rnorm(5), c = rnorm(5), d = rnorm(5) )
How to
You're almost there: you just need to use apply instead of sapply, and remove unnecessary columns.
apply
sapply
apply(y[-1], 1, function(x) cor(x[1:2], x[3:4])
Of course, the correlation between two length-2 vectors isn't very informative....