【问题标题】:Three factor contingency table in RR中的三因素列联表
【发布时间】:2014-11-04 21:18:09
【问题描述】:

我希望为三个主要影响构建一个列联表。这些是犯罪、性别和先前的定罪。响应变量是是否给予了宽大的判决。

这是我迄今为止最好的。

     Crime Gender Priorconv Yes No
1      Shoplifting    Men         N  24  1
2 Other Theft Acts    Men         N  52  9
3      Shoplifting  Women         N  48  3
4 Other Theft Acts  Women         N  22  2
5      Shoplifting    Men         P  17  6
6 Other Theft Acts    Men         P  60 34
7      Shoplifting  Women         P  15  6
8 Other Theft Acts  Women         P   4  3

由以下代码创建

table1<-expand.grid(Crime=factor(c("Shoplifting","Other Theft Acts")),Gender=factor(c("Men","Women")),
Priorconv=factor(c("N","P")))

table1<-data.frame(table1,Yes=c(24,52,48,22,17,60,15,4),No=c(1,9,3,2,6,34,6,3))

不幸的是,这不是很优雅,所以我想知道是否有其他方法可以更清晰地呈现数据。

谢谢。

【问题讨论】:

  • 也许看看xtabs和/或ftable
  • @DominicComtois 我喜欢 xtabs 的功能。我尝试使用 xtabs(cbind(Yes,No)~Crime+Gender+Priorconv,data=table1)
  • 很高兴知道!也许您可以发布代码和输出作为您自己问题的答案。我相信它对您遇到的其他人有用。

标签: r statistics


【解决方案1】:

出于偶然性,您可以使用 sample 运算符并将其放入函数中以更改字符串的数量,例如

factory <- function(i) {
    crime <- sample(c("Shoplifting","Other Theft Acts"),i, replace = TRUE)
    gender <- sample(c("Men","Women"),i,replace = TRUE)
    priorconv <- sample(c("P","N"),i, replace = TRUE)
    table <- data.frame(crime,gender,priorconv)
    return(table)
}
table1 <- factory(20)

结果:

              crime gender priorconv
1       Shoplifting    Men         N
2       Shoplifting  Women         P
3  Other Theft Acts    Men         P
4       Shoplifting    Men         P
5  Other Theft Acts  Women         N
6       Shoplifting  Women         N
7       Shoplifting  Women         P
8       Shoplifting    Men         P
9  Other Theft Acts  Women         P
10      Shoplifting    Men         P
11 Other Theft Acts    Men         N
12 Other Theft Acts    Men         P
13      Shoplifting    Men         P
14      Shoplifting  Women         N
15 Other Theft Acts    Men         N
16 Other Theft Acts    Men         P
17 Other Theft Acts  Women         P
18      Shoplifting  Women         P
19 Other Theft Acts    Men         N
20      Shoplifting  Women         N

【讨论】:

  • 这里的表格有双条目,不是吗?
猜你喜欢
  • 2021-01-28
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 2018-02-12
  • 2013-08-14
  • 2016-07-03
相关资源
最近更新 更多