我正在制作一些人口统计表,包括种族、性别和种族。其中一张表是按种族(西班牙裔/非西班牙裔)划分的性别和种族的交叉表。到目前为止,研究中没有西班牙裔参与者,但需要制作该表并将其发送给相关方(即监管机构)。
但是,我无法为报告制作表格。显然,该表将全为零,但根本没有生成。似乎这是试图计算不存在的东西的限制......
我在下面包含了示例数据:
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) ))
有没有办法产生一个零表?我知道这很愚蠢,但我们必须报告这一点,即使没有这组条件的数据......