By default, the proportions are calculated over the entire table. So each cell describes the proportion of all polygons in that pool with that value of revetment. The whole table sums to 1. Let’s see how to calculate the proportions in R.
prop table in R
The prop.table() is a built-in R function that expresses the table entries as Fraction of Marginal Table. In the prop.table(), the values in each cell are divided by the sum of the 4 cells. If you require proportions across rows or down columns, all you need to do is add the margin argument.
prop.table(m, margin = NULL)
A table like x is expressed relative to margin.
The value of each cell is divided by the sum of the row cells.
The value of each cell is divided by the sum of the column cells.
First, we will create a matrix and then use that matrix to create a prop.table.
To create a matrix in R, use the matrix() function.
mat <- matrix(1:9, 3) print(mat)
[,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9
The next step is to pass this matrix to a prop.table() function and see the output.
mat <- matrix(1:9, 3) pt <- prop.table(mat) print(pt)
[,1] [,2] [,3] [1,] 0.02222222 0.08888889 0.1555556 [2,] 0.04444444 0.11111111 0.1777778 [3,] 0.06666667 0.13333333 0.2000000
Let’s understand how we got this output.
The sum of all the values of the input matrix is = 1 + 2 +3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
Now, for 1st element of the prop table, 1 / 45 = 0.022. In the output, you can see that [1, 1] = 0.0222.
The same for the second element of the prop table: 2/45 = 0.04. You can see that [2, 1] = 0.044.
Now, calculate for the rest of the matrix’s prop values, and you will get the above matrix.
In our example, If you don’t provide a margin, it calculates m / sum(m).
With margins, this is done row- or column-wise.
prop.table() with margins
The second argument of the prop.table() function is margin. There are two types of margin.
margin = 2(column-wise): It sums down columns. Each column sums to 1.
Suppose we want to get prop.table() row-wise, then pass the 1 as an argument.
mat <- matrix(1:9, 3) pt <- prop.table(mat, 1) print(pt)
[,1] [,2] [,3] [1,] 0.08333333 0.3333333 0.5833333 [2,] 0.13333333 0.3333333 0.5333333 [3,] 0.16666667 0.3333333 0.5000000
Let me explain this. The sum of the first row of the matrix is 12. Now divide the first value by 12, and you will get 0.08.
The same for the first row, second value. The input value is 4 and 4 / 12 = 0.03. Same for the first row, third value: 0.5833333.
Calculate the rest values with this approach, and you will get the above output.
If we want to get prop.table() column-wise, then pass the 2 as an argument.
mat <- matrix(1:9, 3) pt <- prop.table(mat, 2) print(pt)
[,1] [,2] [,3] [1,] 0.1666667 0.2666667 0.2916667 [2,] 0.3333333 0.3333333 0.3333333 [3,] 0.5000000 0.4000000 0.3750000
Let me explain this. The sum of the first column of the matrix is 6. Now divide the first value by 6, and you will get 0.166.
The same for the first column, second value. The sum of the first column is 6. The input value is 2 and 2 / 6 = 0.333. Same for the first column, third value: 0.500. Calculate the rest values with this approach, and you will get the above output.
That’s it for prop table() in the R tutorial.
Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.