未加权的表格

Unweighted svytable

我在 R 中使用 survey 包。我正在处理调查数据并使用 svydesign()update() 函数来操作数据集(创建新变量等) .

以下是我获取加权交叉表的方法:

## Build svytable
Drinks_Sex <- svytable(~ Sex + Drinks, design=x)
## Cell Totals
round(addmargins(Drinks_Sex),0)

##         Drinks
## Sex          0     1   Sum
##   Female  6501   213  6714
##   Male    5254   157  5411
##   Sum    11755   370 12125

有没有办法让我使用 survey 包获得 未加权 交叉表?我知道如何在原始数据集上使用 base R 获取未加权的交叉表,但问题是这样做不允许我使用 update().

分析任何变量

或者:我有什么方法可以将我使用 update() 完成的工作传播到原始数据集(csv 格式)中,以便我可以使用 base R 来处理它?

updatetransform的功能相同。如果你只想用 transform 函数替换你所有的 update 函数,同时用 data.frame 替换你的 survey.design 那么它就可以工作了。

library(survey)
data(api)
dclus1<-svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)


# variables in the data.frame object inside the design are here: dclus1$variables
# so
head( dclus1$variables )
# is the same as
head( apiclus1 )

# make whatever new variable
dclus1 <- update( dclus1 , new_col = target * 3 )
# equivalent to
# apiclus1 <- transform( apiclus1 , new_col = target * 3 )
# so long as you are working with a data.frame object instead of a survey.design

# just access the `variables` attribute of the survey.design object
# as if it were a data.frame object.  note: this will not work on some very complicated designs (possibly will not work on database-backed or multiply-imputed or calibrated designs)

# single unweighted table
table( dclus1$variables[ , 'new_col' ] )

# crosstabs
table( dclus1$variables[ , c( 'awards' , 'new_col' ) ] )