I want to analyse the default data set in R (mtcars data set). I am interested in creating column of correlation coefficients according to the below rule. Correlation coefficient of only first three observations ((i.e., row 1,2,3)) between "mpg" and "wt", then leaving the first row, calculate again correlation coefficient between next three observations (i.e., row 2,3,4) between mpg and wt then leaving the first two rows, calculate again correlation coefficient between next three observations (i.e., row 3,4,5) between mpg and wt and so on till end. For example
cor(mtcars$mpg[c(1,2,3)],mtcars$wt[c(1,2,3)])
cor(mtcars$mpg[c(2,3,4)],mtcars$wt[c(2,3,4)])
cor(mtcars$mpg[c(3,4,5)],mtcars$wt[c(3,4,5)]);
and so on. Can anyone help to how to automate this R code using loop etc.
Example, see how i need output, i have done it in excel but i need to do it in R.