# How to find the covariance between two vectors in R?

The covariance is a mathematical measurement to quantify the variation between of two variables together. That means it tells us how two variables under consideration vary together. If we have two vectors and we want to find the covariance between them then we can use the command mentioned below −

cov(“First_Vector”,”Second_Vector”)

## Example 1

Following snippet creates a sample data frame −

x1<-rpois(150,10)
x1

The following dataframe is created

 [1]  3 11 9 6 6 9 11 10 8 9 9 11 9 7 15 7 9 9 9 9 13 9 9 4 8
[26] 8 9 10 9 6 15 8 5 4 3 8 16 12 9 8 13 7 10 13 11 6 10 11 11 13
[51] 17 11 12 10 8 12 12 12 13 9 10 10 14 5 10 9 15 11 12 4 15 9 5 12 15
[76] 5 10 14 11 16 6 7 10 16 11 9 12 14 15 9 12 14 10 15 8 13 5 13 9 6
[101] 6 11 7 8 13 8 9 8 10 11 12 10 7 15 8 11 13 11 11 15 6 9 11 6 4
[126] 11 12 6 9 13 8 9 9 10 11 12 10 12 12 13 9 13 3 11 10 15 13 17 9 9

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x1<-rpois(150,10)
y1<-rpois(150,10)
y1

If you execute all the above given snippets as a single program, it generates the following Output −

  [1] 5 10 6 7 4 9 6 8 14 23 7 10 8 9 7 7 8 10 4 5 13 10 11 5 12
[26] 6 13 7 8 9 15 10 13 13 9 13 6 12 13 14 4 3 16 13 6 13 11 12 14 10
[51] 9 19 9 11 6 10 14 13 8 12 11 5 11 8 9 8 8 10 14 15 8 7 8 10 3
[76] 10 12 9 15 13 14 7 11 5 4 11 11 17 8 8 7 12 13 6 16 12 11 10 10 7
[101] 15 12 7 15 12 14 11 15 8 10 7 11 10 19 5 16 10 7 6 5 11 8 9 13 8
[126] 15 12 5 9 10 5 8 10 11 14 14 9 15 9 8 9 5 6 10 10 10 9 9 12 6

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x1<-rpois(150,10)
y1<-rpois(150,10)
cov(x1,y1)

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.4774049

## Example 2

Following snippet creates a sample data frame −

x2<-rnorm(50)
x2

The following dataframe is created

[1]   1.25750902  0.32186949  3.40052558  0.44119671 -1.08329511 -0.26017558
[7]   0.57312232 -0.26519546  0.09472348  1.28948733 -0.19954613  0.05224845
[13] -0.67385227 -0.51864208  1.02551846  0.50382941 -0.62157377  0.69820899
[19]  0.88856349 -0.61695761 -0.48919541  1.34682557 -2.18372173  1.07146847
[25] -0.48230712 -0.41690160 -0.38002286  1.44233750  0.38448766 -0.01916229
[31]  0.44851256 -0.41081087  0.75761904 -0.90755448  1.92020781  0.50809101
[37]  1.34417231 -0.30847061  0.42927524  1.23221216  0.76175556 -0.98098577
[43] -0.36721302  1.58770646 -1.30875036 -2.03476277 -0.25771345  0.47015599
[49]  0.40688749 -0.52790950

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x2<-rnorm(50)
y2<-rnorm(50)
y2

If you execute all the above given snippets as a single program, it generates the following Output −

[1]  0.98125028   0.34346184  1.72634033 -0.17178732  0.38358028 -1.32951022
[7]  0.31342774   1.14990770 -0.31250392  0.60410350 -0.78426947  0.22188330
[13] -0.49964967 -0.76936102 -1.49598364  1.17451928  1.36855537  0.77550430
[19] -0.61261281  2.30931171 -1.06114800 -0.73770636 -0.55790321 -1.01225363
[25]  2.04464287 -0.42538498  0.84938564  0.19667487 -0.42453258  0.93404438
[31] -0.41025984  1.05187427 -0.49740459  0.07785702 -1.07441547 -1.73279495
[37] -1.81296164 -0.53280769  0.78350484 -0.73800794  0.11623244  0.45957466
[43] -0.28623314  0.78454369 -0.83363845 -1.19011695 -0.77571641 -1.87916329
[49] -0.06926718  1.34875928

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x2<-rnorm(50)
y2<-rnorm(50)
cov(x2,y2)

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.01245908

## Example 3

Following snippet creates a sample data frame −

x3<-sample(0:9,200,replace=TRUE)
x3

The following dataframe is created

[1] 1 1 5 2 9 7 7 1 6 6 6 7 0 7 1 2 8 2 0 9 3 6 0 4 4 3 3 3 8 3 4 1 0 9 4 1 6
[38] 7 0 9 3 4 4 9 8 0 0 5 2 5 9 7 4 0 8 9 4 0 7 8 1 8 5 7 3 1 8 0 5 4 7 1 3 5
[75] 8 7 4 3 7 7 9 2 5 9 8 6 2 2 7 1 4 7 8 0 0 1 4 9 7 5 2 8 0 0 1 4 6 7 0 9 7
[112] 3 2 3 7 4 8 8 9 2 2 5 5 8 4 6 7 4 4 5 0 0 9 5 3 1 7 0 1 7 4 1 2 7 8 4 9 2
[149] 7 2 1 5 9 4 4 3 1 7 7 4 7 6 5 3 8 1 0 7 3 0 7 0 1 1 5 6 1 1 2 4 1 5 0 1 1
[186] 3 8 1 6 5 0 3 6 3 2 2 3 4 6 7

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x3<-sample(0:9,200,replace=TRUE)
y3<-sample(0:9,200,replace=TRUE)
y3

If you execute all the above given snippets as a single program, it generates the following Output −

  [1] 9 8 7 4 2 4 7 0 8 7 6 8 7 6 1 7 4 4 2 6 5 2 8 0 4 0 1 2 0 7 6 9 9 6 5 5 2
[38] 5 4 9 3 7 8 2 5 5 7 4 1 0 3 6 0 5 1 5 8 1 1 8 3 8 2 9 2 7 0 1 8 6 3 1 4 3
[75] 7 1 8 5 3 6 0 5 2 3 3 3 8 6 8 2 6 1 6 6 4 9 0 5 2 2 8 6 7 3 4 8 9 5 3 9 6
[112] 4 6 7 0 9 3 3 5 9 0 8 5 2 8 3 9 3 3 8 9 4 9 6 3 8 7 2 7 0 8 1 6 8 2 5 8 4
[149] 1 7 8 7 3 0 8 4 2 2 5 4 7 1 4 2 1 8 7 9 4 3 1 5 6 5 1 5 8 6 7 1 9 9 7 5 9
[186] 9 5 4 7 7 4 2 1 1 8 4 6 7 5 8

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x3<-sample(0:9,200,replace=TRUE)
y3<-sample(0:9,200,replace=TRUE)
cov(x3,y3)

If you execute all the above given snippets as a single program, it generates the following Output −

[1] -0.6922111

## Example 4

Following snippet creates a sample data frame −

x4<-sample(1:100,200,replace=TRUE)
x4

The following dataframe is created

  [1] 68 42 76 20 68 9 47 84 31 91 72 30 32 28 78 88 27 50
[19] 46 54 37 20 10 1 34 16 54 61 99 87 25 8 80 11 53 85
[37] 70 57 71 75 100 39 17 54 66 20 18 59 23 96 85 27 45 65
[55] 63 54 64 98 70 30 43 96 52 48 5 12 8 57 61 72 92 27
[73] 21 30 36 22 83 17 56 6 55 67 92 85 57 95 3 90 20 88
[91] 70 87 14 3 76 70 100 32 58 26 93 43 48 90 42 77 92 88
[109] 3 35 92 96 77 45 19 36 76 61 68 12 73 39 73 11 35 71
[127] 70 9 50 46 53 15 48 47 56 44 15 49 33 66 67 33 77 62
[145] 38 56 49 33 93 72 39 74 61 10 94 49 84 79 80 35 67 65
[163] 17 97 13 91 80 42 50 74 55 70 24 66 91 54 32 19 23 100
[181] 50 60 16 21 37 31 26 32 75 85 35 82 77 59 70 3 14 88
[199] 75 76

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x4<-sample(1:100,200,replace=TRUE)
y4<-sample(1:100,200,replace=TRUE)
y4

If you execute all the above given snippets as a single program, it generates the following Output −

  [1] 49 59 93 12 21 1 36 45 62 85 19 88 5 74 7 63 42 90
[19] 70 66 20 61 22 63 94 67 59 22 86 48 50 83 64 12 59 33
[37] 11 74 8 99 93 23 18 58 83 82 68 57 71 52 77 98 97 38
[55] 54 81 32 94 79 3 52 40 41 42 61 54 11 64 44 90 17 63
[73] 11 61 3 46 2 70 3 62 41 13 53 80 16 75 86 60 11 38
[91] 72 83 17 63 54 52 77 67 65 25 91 2 79 93 17 63 36 3
[109] 47 32 51 17 30 12 95 40 57 18 99 87 56 66 45 89 30 52
[127] 20 92 24 30 58 54 88 61 73 15 22 69 82 3 96 42 85 58
[145] 63 70 91 98 91 46 4 23 67 89 7 92 10 10 77 31 80 34
[163] 45 74 46 65 88 86 15 32 10 32 17 97 45 88 44 69 85 35
[181] 45 85 51 80 46 44 65 63 16 28 62 53 47 60 63 55 34 73
[199] 50 100

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x4<-sample(1:100,200,replace=TRUE)
y4<-sample(1:100,200,replace=TRUE)
cov(x4,y4)

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 24.04623

## Example 5

Following snippet creates a sample data frame −

x5<-runif(50,2,5)
x5

The following dataframe is created

 [1] 3.262035 3.831783 3.212780 3.566762 2.076127 4.933796 2.550656 3.561904
[9] 2.907720 3.889833 2.777207 3.249720 4.521201 4.310791 4.713148 3.620999
[17] 3.985192 3.290758 3.449575 2.213117 3.644794 2.694455 4.911007 4.932060
[25] 2.757384 3.337899 3.946829 3.663683 2.738155 4.050442 2.351040 3.209593
[33] 2.713664 4.359059 3.947258 3.316234 4.118935 4.564338 3.251211 3.264638
[41] 4.674579 2.970812 3.063797 3.577531 3.954324 3.663500 4.248933 3.761611
[49] 3.891069 3.178017

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x5<-runif(50,2,5)
y5<-runif(50,2,5)
y5

If you execute all the above given snippets as a single program, it generates the following Output −

 [1] 2.777017 3.811627 3.592484 4.513459 4.327442 4.184489 3.913186 2.771264
[9] 4.172619 2.781835 2.728659 4.343730 3.743249 2.264408 4.294990 4.179432
[17] 3.576398 4.898790 4.094172 3.182964 3.207227 3.559252 4.996966 4.390270
[25] 3.637017 2.330806 3.220873 3.372030 2.680986 2.166316 4.752359 2.517513
[33] 3.814364 4.396744 2.047065 3.522479 4.422268 3.351226 4.109004 4.028507
[41] 4.150114 2.140215 4.459092 2.227658 4.041431 3.735306 3.122045 4.577403
[49] 4.851432 3.479120

To find the covariance between two vectors, on the above created data frame, add the following code to the above snippet −

x5<-runif(50,2,5)
y5<-runif(50,2,5)
cov(x5,y5)

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.06237247