title: How to create covariance matrix in R?
body: <p>I'm trying to build covariance matrix from a scratch (cov() function). My task is not to use any package. Hence I created my functions:</p>
  
  <pre><code>meanf &lt;- function(x){
  sum(x) / length(x)
  }
  
  sampleCov &lt;- function(x,y){
    stopifnot(identical(length(x), length(y)))
    sum((x - meanf(x)) * (y - meanf(y))) / (length(x) - 1)
  }
  
  &gt; sampleCov(winequality_red$quality, winequality_red$alcohol)
  [1] 0.409789
  </code></pre>
  
  <p>Unfortunately, I'm stuck here. All loops I tried to apply are missing any point. Of course it's possible to just copy the sampleCov function and make it for every possible combination but that's not my point.</p>