# How to Calculate Cramer’s V in R

How to Calculate Cramer’s V in R, Cramer’s V is a statistic that ranges from 0 to 1 and is used to assess the strength of the relationship between two nominal variables.

Closer values near 0 suggest a weak relationship between the variables, while closer values to 1 indicate a strong relationship.

The following formula is used to calculate Cramer’s V statistic.

`Cramer’s V = √(X2/n) / min(c-1, r-1)`

where:

X2: The Chi-square statistic

n: Total sample size

r: Number of rows

c: Number of columns

## How to Calculate Cramer’s V in R

This article shows how to calculate Cramer’s V for a contingency table in R using a couple of examples.

### Approach 1: Cramer’s V for a 2×2 Table

The following code explains how to calculate Cramer’s V for a 22 table using the CramerV function from the rcompanion package.

Let’s create a 2×2 table matrix,

`data = matrix(c(5,8,10,23), nrow = 2)`

Now we can view the matrix

`data`
```     [,1] [,2]
[1,]    5   10
[2,]    8   23```

Load the rcompanion library in the R console.

`library(rcompanion)`

Now it’s ready to calculate Cramer’s V statistic

`cramerV(data)`
```Cramer V
0.07836```

Cramer’s V is 0. 07836, indicating a rather weak relationship between the two variables in the table.

By using ci = TRUE, we can also generate a confidence interval for Cramer’s V:

`cramerV(data, ci = TRUE)`
```  Cramer.V lower.ci upper.ci
1  0.07836 0.004985   0.3639```

Cramer’s V stays constant at 0. 07836, but we now have a 95 percent confidence interval containing a range of values that is likely to reflect Cramer’s V’s true value.

[0.004985, 0.3639] turns out to be the interval.

### Approach 2: Cramer’s V for Larger Tables

It’s worth noting that the CramerV function can be used to calculate Cramer’s V for any table size.

For a table with two rows and three columns, the following code explains how to calculate Cramer’s V.

Let’s create a 3×4 table

`data<-matrix(c(3, 9, 14, 15, 12, 22,10,16,9,10,14,12), nrow = 3)`

Let’s view the matrix

`data`
```   [,1] [,2] [,3] [,4]
[1,]    3   15   10   10
[2,]    9   12   16   14
[3,]   14   22    9   12```

Now we can calculate Cramer’s V

`cramerV(data)`
```Cramer V
0.1781```

Cramer’s V turns out to be 0.1781, indicating a rather weak relationship between the two variables in the table, but better than approach 1.

`cramerV(data, ci = TRUE)`
```Cramer.V lower.ci upper.ci
1   0.1781    0.127    0.313```

How to perform ANCOVA in R » Quick Guide