- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to add proportion total at margins on a table in R?
The proportion total in a table helps us to understand the contribution of each row and each column in the total. Therefore, if we want to find the proportion total at margins, we can use addmargins function if we have the proportion table and if we do not have that table then firstly it needs to be created and then use the addmargins function. For example, if we have a proportion table called prop then the command will be addmargins(prop).
Example1
Consider the below table of proportions −
> x1<-rpois(5,2) > x2<-rpois(5,2) > x3<-rpois(5,2) > x4<-rpois(5,2) > x5<-rpois(5,2) > x6<-rpois(5,2) > x7<-rpois(5,2) > x8<-rpois(5,2) > table1<-prop.table(rbind(x1,x2,x3,x4,x5,x6,x7,x8)) > table1
Output
[,1] [,2] [,3] [,4] [,5] x1 0.07692308 0.01538462 0.01538462 0.00000000 0.06153846 x2 0.01538462 0.01538462 0.03076923 0.01538462 0.01538462 x3 0.00000000 0.01538462 0.01538462 0.01538462 0.01538462 x4 0.06153846 0.00000000 0.07692308 0.00000000 0.03076923 x5 0.06153846 0.01538462 0.03076923 0.03076923 0.00000000 x6 0.03076923 0.06153846 0.03076923 0.01538462 0.01538462 x7 0.00000000 0.04615385 0.00000000 0.01538462 0.03076923 x8 0.04615385 0.01538462 0.00000000 0.01538462 0.04615385
Adding margins to table1 −
> addmargins(table1)
Output
Sum x1 0.07692308 0.01538462 0.01538462 0.00000000 0.06153846 0.16923077 x2 0.01538462 0.01538462 0.03076923 0.01538462 0.01538462 0.09230769 x3 0.00000000 0.01538462 0.01538462 0.01538462 0.01538462 0.06153846 x4 0.06153846 0.00000000 0.07692308 0.00000000 0.03076923 0.16923077 x5 0.06153846 0.01538462 0.03076923 0.03076923 0.00000000 0.13846154 x6 0.03076923 0.06153846 0.03076923 0.01538462 0.01538462 0.15384615 x7 0.00000000 0.04615385 0.00000000 0.01538462 0.03076923 0.09230769 x8 0.04615385 0.01538462 0.00000000 0.01538462 0.04615385 0.12307692 Sum 0.29230769 0.18461538 0.20000000 0.10769231 0.21538462 1.00000000
Example2
> y1<-rpois(20,4) > y2<-rpois(20,4) > y3<-rpois(20,4) > df_y<-data.frame(y1,y2,y3) > df_y
Output
y1 y2 y3 1 6 3 4 2 6 6 8 3 4 3 5 4 5 6 3 5 2 3 1 6 4 4 5 7 2 4 7 8 2 1 3 9 8 6 6 10 2 5 4 11 3 7 1 12 3 3 4 13 6 4 3 14 4 3 1 15 2 3 2 16 2 1 6 17 4 5 2 18 4 5 3 19 7 6 5 20 3 5 3
Example
> table2<-prop.table(as.matrix(df_y)) > table2
Output
y1 y2 y3 [1,] 0.025210084 0.012605042 0.016806723 [2,] 0.025210084 0.025210084 0.033613445 [3,] 0.016806723 0.012605042 0.021008403 [4,] 0.021008403 0.025210084 0.012605042 [5,] 0.008403361 0.012605042 0.004201681 [6,] 0.016806723 0.016806723 0.021008403 [7,] 0.008403361 0.016806723 0.029411765 [8,] 0.008403361 0.004201681 0.012605042 [9,] 0.033613445 0.025210084 0.025210084 [10,] 0.008403361 0.021008403 0.016806723 [11,] 0.012605042 0.029411765 0.004201681 [12,] 0.012605042 0.012605042 0.016806723 [13,] 0.025210084 0.016806723 0.012605042 [14,] 0.016806723 0.012605042 0.004201681 [15,] 0.008403361 0.012605042 0.008403361 [16,] 0.008403361 0.004201681 0.025210084 [17,] 0.016806723 0.021008403 0.008403361 [18,] 0.016806723 0.021008403 0.012605042 [19,] 0.029411765 0.025210084 0.021008403 [20,] 0.012605042 0.021008403 0.012605042
Adding margins to table2 −
> addmargins(table2)
Output
y1 y2 y3 Sum 0.025210084 0.012605042 0.016806723 0.05462185 0.025210084 0.025210084 0.033613445 0.08403361 0.016806723 0.012605042 0.021008403 0.05042017 0.021008403 0.025210084 0.012605042 0.05882353 0.008403361 0.012605042 0.004201681 0.02521008 0.016806723 0.016806723 0.021008403 0.05462185 0.008403361 0.016806723 0.029411765 0.05462185 0.008403361 0.004201681 0.012605042 0.02521008 0.033613445 0.025210084 0.025210084 0.08403361 0.008403361 0.021008403 0.016806723 0.04621849 0.012605042 0.029411765 0.004201681 0.04621849 0.012605042 0.012605042 0.016806723 0.04201681 0.025210084 0.016806723 0.012605042 0.05462185 0.016806723 0.012605042 0.004201681 0.03361345 0.008403361 0.012605042 0.008403361 0.02941176 0.008403361 0.004201681 0.025210084 0.03781513 0.016806723 0.021008403 0.008403361 0.04621849 0.016806723 0.021008403 0.012605042 0.05042017 0.029411765 0.025210084 0.021008403 0.07563025 0.012605042 0.021008403 0.012605042 0.04621849 Sum 0.331932773 0.348739496 0.319327731 1.00000000
Advertisements