gpt4 book ai didi

r - R中交叉表一侧的多个变量

转载 作者:行者123 更新时间:2023-12-02 04:53:47 24 4
gpt4 key购买 nike

我有一个看起来像这样的数据集:

ID  wts     S2      S5.1    S5.2    S5.3
42 0.78 Male Yes No Yes
45 1.22 Female No Yes No
48 0.98 Male Yes Yes Yes
49 1.11 Female Yes Yes No
51 1.21 Male Yes Yes No

我正在尝试使用“descr”包中的“crosstab”函数创建一个加权表。我有一个基本表,可以使用以下行正常工作:

crosstab(*fileName*$S5.1,*fileName*$S2,weight=wts,prop.c=T) 

但我真正想做的是将所有 3 个二进制变量都放在表格的 y 轴下方。我环顾四周好几天了,无法弄清楚。任何帮助,将不胜感激!如果该信息,我正在通过 SPSS 文件提取数据。有帮助。

如果这是一个非常简单的问题,我们深表歉意。 R 的新手。


dput(head(data))


structure(list(ID = c(42, 45, 48, 49, 51), wts = c(0.78, 1.22,
0.98, 1.11, 1.21), S2 = structure(c(1L, 2L, 1L, 2L, 1L), .Label = c("Male",
"Female"), class = "factor"), S5.1 = structure(c(2L, 1L, 2L,
2L, 2L), .Label = c("No", "Yes"), class = "factor"), S5.2 = structure(c(1L,
2L, 2L, 2L, 2L), .Label = c("No", "Yes"), class = "factor"),
S5.3 = structure(c(2L, 1L, 2L, 1L, 1L), .Label = c("No",
"Yes"), class = "factor")), .Names = c("ID", "wts", "S2",
"S5.1", "S5.2", "S5.3"), variable.labels = structure(c("", "",
"Gender", "Dog?", "Cat?", "Bird?"), .Names = c("ID", "wts", "S2",
"S5.1", "S5.2", "S5.3")), codepage = 1252L, row.names = c(NA,
5L), class = "data.frame")

我希望得到类似这样的东西...(其中添加的第二个和第三个是/否分别是 S5.2 和 S5.3)。谢谢!

==========================================
temp.spss$S2
temp.spss$S5.1 Male Female Total
------------------------------------------
No 0 1 1
0.000 50.000
------------------------------------------
Yes 3 1 4
100.000 50.000
------------------------------------------
No 1 0 1
33.333 00.000
------------------------------------------
Yes 2 2 4
66.666 100.000
------------------------------------------
No 1 2 3
33.333 100.000
------------------------------------------
Yes 2 0 2
66.666 0.000
------------------------------------------
Total 9 6 15
180.000 120.000
==========================================

最佳答案

也许使用交互(并且猜测您不希望条目全为零的行:

install.packages("descr")
descr::crosstab( with(dat,
interaction(S5.1, S5.2, S5.3, drop=TRUE)) ,dat$S2,weight=dat$wts,prop.c=T)

查看结果后,我发现迫切需要审查结果的敏感性。

#+-------------------------------------------------------
Cell Contents
|-------------------------|
| Count |
| Column Percent |
|-------------------------|

================================================================================
dat$S2
with(dat, interaction(S5.1, S5.2, S5.3, drop = TRUE)) Female Male Total
--------------------------------------------------------------------------------
No.Yes.No 1 0 1
50.000 0.000
--------------------------------------------------------------------------------
Yes.Yes.No 1 1 2
50.000 33.333
--------------------------------------------------------------------------------
Yes.No.Yes 0 1 1
0.000 33.333
--------------------------------------------------------------------------------
Yes.Yes.Yes 0 1 1
0.000 33.333
--------------------------------------------------------------------------------
Total 2 3 5
40.000 60.000
================================================================================
>

关于r - R中交叉表一侧的多个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25170440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com