如何在R中的表上的边距处增加比例总数?
表格中的总计比例有助于我们了解总计中每一行和每一列的贡献。因此,如果我们想在边距处找到比例总和,如果我们有比例表,则可以使用addmargins函数;如果没有该表,则首先需要创建它,然后再使用addmargins函数。例如,如果我们有一个称为prop的比例表,则命令将为addmargins(prop)。
例1
考虑下表的比例-
> 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输出结果
[,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
在表1中增加边距-
> addmargins(table1)输出结果
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
例2
> y1<-rpois(20,4) > y2<-rpois(20,4) > y3<-rpois(20,4) > df_y<-data.frame(y1,y2,y3) > df_y输出结果
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
示例
> table2<-prop.table(as.matrix(df_y)) > table2输出结果
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
在表2中增加边距-
> addmargins(table2)输出结果
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