**Covariance** and **Correlation** are terms used in statistics to measure relationships between two random variables. Both of these terms measure linear dependency between a pair of random variables or bivariate data.

In this article, we are going to discuss ** cov()**,

**and**

`cor()`

**functions in R which use covariance and correlation methods of statistics and probability theory.**

`cov2cor()`

#### Covariance

In R programming, covariance can be measured using

function. Covariance is a statistical term used to measures the direction of the linear relationship between the data vectors. Mathematically, **cov()**

**where,**

xrepresents the x data vector

yrepresents the y data vector

represents mean of x data vector

represents mean of y data vector

Nrepresents total obeservations

**Syntax:**

cov(x, y, method)

**where,**

xandyrepresents the data vectorsmethoddefines the type of method to be used to compute covariance. Default is "pearson".

**Example:**

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

*chevron_right*

*filter_none*

**Output:**

[1] 30.66667 [1] 30.66667 [1] 12 [1] 1.666667

#### Correlation

function in R programming measures the correlation coefficient value. Correlation is a relationship term in statistics that uses the covariance method to measure how strong the vectors are related. Mathematically,**cor()**

**where,**

xrepresents the x data vector

yrepresents the y data vector

represents mean of x data vector

represents mean of y data vector

**Syntax:**

cor(x, y, method)

**where,**

xandyrepresents the data vectorsmethoddefines the type of method to be used to compute covariance. Default is "pearson".

**Example:**

`# Data vectors ` `x <` `-` `c(` `1` `, ` `3` `, ` `5` `, ` `10` `) ` ` ` `y <` `-` `c(` `2` `, ` `4` `, ` `6` `, ` `20` `) ` ` ` `# Print correlation using different methods ` `print` `(cor(x, y)) ` ` ` `print` `(cor(x, y, method ` `=` `"pearson"` `)) ` ` ` `print` `(cor(x, y, method ` `=` `"kendall"` `)) ` ` ` `print` `(cor(x, y, method ` `=` `"spearman"` `)) ` |

*chevron_right*

*filter_none*

**Output:**

[1] 0.9724702 [1] 0.9724702 [1] 1 [1] 1

#### Conversion of Covariance to Correlation

function in R programming converts a covariance matrix into corresponding correlation matrix.**cov2cor()**

**Syntax:**

cov2cor(X)

**where,
X** and

**y**represents the covariance square matrix

**Example:**

`# Data vectors ` `x <` `-` `rnorm(` `2` `) ` `y <` `-` `rnorm(` `2` `) ` ` ` `# Binding into square matrix ` `mat <` `-` `cbind(x, y) ` ` ` `# Defining X as the covariance matrix ` `X <` `-` `cov(mat) ` ` ` `# Print covariance matrix ` `print` `(X) ` ` ` `# Print correlation matrix of data vector ` `print` `(cor(mat)) ` ` ` `# Using function cov2cor() ` `# To convert covariance matrix to correlation matrix ` `print` `(cov2cor(X)) ` |

*chevron_right*

*filter_none*

**Output:**

x y x 0.0742700 -0.1268199 y -0.1268199 0.2165516 x y x 1 -1 y -1 1 x y x 1 -1 y -1 1

## Recommended Posts:

- Compute the Covariance between Two Vectors in R Programming - cov() Function
- Compute the Correlation Coefficient Value between Two Vectors in R Programming - cor() Function
- Spearman Correlation Testing in R Programming
- Kendall Correlation Testing in R Programming
- Pearson Correlation Testing in R Programming
- Visualize correlation matrix using correlogram in R Programming
- Visualize Correlation Matrix using symnum function in R Programming
- Correlation Matrix in R Programming
- Compute Variance and Standard Deviation of a value in R Programming - var() and sd() Function
- Calculate the Floor and Ceiling values in R Programming - floor() and ceiling() Function
- Naming Rows and Columns of a Matrix in R Programming - rownames() and colnames() Function
- Get Date and Time in different Formats in R Programming - date(), Sys.Date(), Sys.time() and Sys.timezone() Function
- Compute the Parallel Minima and Maxima between Vectors in R Programming - pmin() and pmax() Functions
- Add Leading Zeros to the Elements of a Vector in R Programming - Using paste0() and sprintf() Function
- Absolute and Relative Frequency in R Programming
- Skewness and Kurtosis in R Programming
- Convert Factor to Numeric and Numeric to Factor in R Programming
- Grid and Lattice Packages in R Programming
- Logarithmic and Power Functions in R Programming
- Getting and Setting Length of the Vectors in R Programming - length() Function

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.