1

我正在制作一些人口统计表,包括种族、性别和种族。其中一张表是按种族(西班牙裔/非西班牙裔)划分的性别和种族的交叉表。到目前为止,研究中没有西班牙裔参与者,但需要制作该表并将其发送给相关方(即监管机构)。

但是,我无法为报告制作表格。显然,该表将全为零,但根本没有生成。似乎这是试图计算不存在的东西的限制......

我在下面包含了示例数据:


race.in <- read.table(
text = "race eth sex
b   n   f
b   n   f
b   n   f
w   n   f
w   n   m
w   n   m
a   n   m
a   n   m
a   n   f
ai  n   m
ai  n   f
ai  n   m", header = TRUE)

attach(race.in)

race.levels <- c("b", "w", "a", "ai", "nh") 
eth.levels  <- c("h", "n")  # hispanic , not hispanic
sex.levels  <- c("m", "f")


#  this table is fine
table(factor(race, levels = race.levels), factor(sex, levels = sex.levels) )

#  this table is fine
table(factor(eth, levels = eth.levels), factor(sex, levels = sex.levels) )

#  table of race and ethnicity by sex
by(race.in, sex, FUN = function(X)  table(factor(race, levels = race.levels), factor(eth, levels = eth.levels) ))  

#  produces NULL for table for levels of "h"
by(race.in, factor(eth, levels = eth.levels), FUN = function(X)  table(factor(race, levels = race.levels), factor(sex, levels = sex.levels) ))

有没有办法产生一个零表?我知道这很愚蠢,但我们必须报告这一点,即使没有这组条件的数据......

4

1 回答 1

2

我不清楚为什么您不只是将变量考虑在内data.frame。这使得创建表格变得更加容易。

race.in$race <- factor(race.in$race, race.levels)
race.in$eth <- factor(race.in$eth, eth.levels)
race.in$sex <- factor(race.in$sex, sex.levels)
table(race.in)
table(race.in[c(1, 3, 2)])
# , , eth = h
# 
#     sex
# race m f
#   b  0 0
#   w  0 0
#   a  0 0
#   ai 0 0
#   nh 0 0
# 
# , , eth = n
# 
#     sex
# race m f
#   b  0 3
#   w  2 1
#   a  2 1
#   ai 2 1
#   nh 0 0

您可能也有兴趣探索该ftable功能(对于“平面”表)。例如:

> ftable(x=race.in, row.vars=1, col.vars=2:3)
     eth h   n  
     sex m f m f
race            
b        0 0 0 3
w        0 0 2 1
a        0 0 2 1
ai       0 0 2 1
nh       0 0 0 0
于 2013-04-17T16:49:55.887 回答