Compute the Covariance between Two Vectors in R Programming – cov() Function

function in R Language is used to measure the covariance between two vectors.**cov()**

Syntax:cov(x, y, method)

Parameters:x, y:Data vectorsmethod:Type of method to be used

**Example 1:**

`# Data vectors ` `x <` `-` `c(` `1` `, ` `3` `, ` `5` `, ` `10` `) ` ` ` `y <` `-` `c(` `2` `, ` `4` `, ` `6` `, ` `20` `) ` ` ` `# Print covariance using Pearson method` `print` `(cov(x, y, method ` `=` `"pearson"` `)) ` |

**Output:**

[1] 30.66667

**Example 2:**

`# Data vectors ` `x <` `-` `c(` `1` `, ` `3` `, ` `5` `, ` `10` `) ` ` ` `y <` `-` `c(` `2` `, ` `4` `, ` `6` `, ` `20` `) ` ` ` `# Print covariance using Pearson method` `print` `(cov(x, y, method ` `=` `"kendall"` `)) ` |

**Output:**

[1] 12

**Example 3:**

`# Data vectors ` `x <` `-` `c(` `1` `, ` `3` `, ` `5` `, ` `10` `) ` ` ` `y <` `-` `c(` `2` `, ` `4` `, ` `6` `, ` `20` `) ` ` ` `# Print covariance using Pearson method` `print` `(cov(x, y, method ` `=` `"spearman"` `)) ` |

**Output:**

[1] 1.666667

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.