使用R,可以获得没有数据的频率表吗? [英] Using R, is it possible to get a frequency table where no data exist?

查看:60
本文介绍了使用R,可以获得没有数据的频率表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am producing some demographic tables, to include race, sex, and ethnicity. One of the tables is a crosstab of sex and race by ethnicity (Hispanic / not Hispanic). So far, there are no Hispanic participants in the study, but the table needs to be produced and sent to interested parties (i.e., regulatory agencies).

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

However, I have not been able to produce a table for the report. Obviously, the table would be all zeroes, but it is not being produced at all. It seems that this is a limitation of trying to calculate something that does not exist...

我在下面提供了示例数据:

I have included example data below:

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) ))


有什么方法可以产生零表吗?我知道这很愚蠢,但是即使没有针对这组条件的数据,我们也必须对此进行举报...


Is there any way to produce a table of zeroes? I know it's silly, but we have to report this, even though there is no data for this set of conditions...

推荐答案

我不清楚您为什么不只将变量包含在data.frame中.这使创建表变得更加容易.

I'm not clear why you don't just factor your variables in your data.frame. That makes creating tables much easier.

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函数(用于平面"表)感兴趣.例如:


You may also be interested in exploring the ftable function (for "flat" tables). For example:

> 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

这篇关于使用R,可以获得没有数据的频率表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆