0

I have 2 large set of data, each have 2000+ data and trying to find the covariance for every 5 row.

x=c(1,2,3,4,5)
y=c(6,7,8,9,10)
df=data.frame(x,y)
group=rep(1:length(df),each=2,length=length(df))

What is my next step so I can find the covariance like this`

cov(x[1:2,],y[1:2,])

and

cov(x[3:4,],y[3:4,])
Ian
  • 3
  • 2
  • 2
    There are a few mistakes in your post, @Ian. `a` is not defined, `x` and `y` are vectors at the beginning, but later on they are not, you have a `}` instead of `]`. Also, the question is not very clear. – kangaroo_cliff Jul 18 '17 at 02:46
  • Sorry. I just edit it and is that better? – Ian Jul 18 '17 at 03:22

1 Answers1

0
library(zoo)    
x = c(1,2,3,4,5)
y = c(6,7,8,9,10)
rows = 2
out = rollapply(data.frame(x,y), rows, function(x) cov(x[,1],x[,2]),
                    by.column=FALSE)
out
AK88
  • 2,946
  • 2
  • 12
  • 31
  • Would you mind explain it a little bit with rollapply? I tried in my data.frame and my covariance is all the same result for some reason :( – Ian Jul 18 '17 at 04:06
  • basically, `rollapply` applies `cov()` function for each `n` rows in the data frame of `x` and `y` (hence `data.frame(x,y)`). You are getting the same values (`0.5`'s I assume?) because of your data. Try to change `x = rnorm(5, 0, 2)` and `y = rnorm(5, 0, 3)`, you will see the difference. – AK88 Jul 18 '17 at 04:13
  • Thank you very much for your help. I have got the solution out :D – Ian Jul 18 '17 at 13:44