Contingency table – 如何获取行总数

huangapple go评论63阅读模式
英文:

Contingency table - how to obtain the row total

问题

我使用mtcars数据集生成一个2x2的表格,其中包括vs变量(二进制0/1变量)和cyl变量(3个类别)。我目前有一个列联表,其中单元格总和为整个表格的100%。我试图获取行总百分比,即在给定cyl变量类别的情况下取值为0或1的观测百分比。

summary(mtcars)
typeof(mtcars$cyl)
table(mtcars$cyl)
contingency_table <- prop.table(table(mtcars$vs, mtcars$cyl))

view(contingency_table)
英文:

I using the mtcars data set to produce a 2x2 table of vs var (binary 0/1 variables) and cyl var (3 categories). I currently have a contingency table where the cells sum to 100% in the entire table. I am trying to obtain the row total percent that is the percent of observations taking a value of 0 or 1, given a cyl var category

  summary(mtcars)
  typeof(mtcars$cyl)
  table(mtcars$cyl)
  contingency_table &lt;- prop.table(table(mtcars$vs, mtcars$cyl))

  view(contingency_table)

答案1

得分: 1

使用mtcars数据集,创建一个包含两个表格的数据框,其中第一个表格是(vs, cyl)的频数表的比例,第二个表格是vs的频数表的比例。

        4       6      8       
0 0.03125 0.09375 0.4375 0.5625
1 0.31250 0.12500 0.0000 0.4375

或者如果已经有contingency_table

contingency_table与行总计(rowSums)合并。

如果需要边际百分比,请指定margin参数(1表示按行,2表示按列):

使用mtcars数据集,创建(vs, cyl)的频数表的比例,指定`margin`参数为2
             4          6          8
  0 0.09090909 0.42857143 1.00000000
  1 0.90909091 0.57142857 0.00000000
英文:
with(mtcars, cbind(prop.table(table(vs, cyl)), prop.table(table(vs))))
        4       6      8       
0 0.03125 0.09375 0.4375 0.5625
1 0.31250 0.12500 0.0000 0.4375

Or if you already have contingency_table:

cbind(contingency_table, rowSums(contingency_table))

If you want marginal percentages, specify the margin argument (1 for rows, 2 for columns):

prop.table(table(mtcars$vs, mtcars$cyl), margin = 2)
             4          6          8
  0 0.09090909 0.42857143 1.00000000
  1 0.90909091 0.57142857 0.00000000

huangapple
  • 本文由 发表于 2023年8月4日 05:30:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定