消除R中CrossTable中的值
我刚刚开始学习 R,我正在努力将我的注意力集中在卡方上以完成大学作业。
具体来说,我正在使用 2018 年一般社会调查数据集(对于密码本: https:/ /www.thearda.com/Archive/Files/Codebooks/GSS2018_CB.asp),我试图弄清楚宗教是否对人们寻求心理健康帮助的方式
我想使用reliten
(宗教信仰的自我评估 - 从强烈到无宗教)作为自变量,以及mentloth
(询问有心理健康问题的人是否应该这样做)联系心理健康专业人士(是或否)作为因变量。在卡方旁边,我还想添加 CrossTable(GSS18$reliten, GSS18$mentloth)
,但我不知道如何去掉“不适用”、“不适用”知道”和“无响应”值编码为 0、8 和 9。有人有一些提示吗?
下面是我的数据的简短预览,如果有帮助的话。
structure(list(reliten = structure(c(1, 1, 4, 1, 1, 2, 1, 1,
4, 2, 2, 3, 2, 2, 4, 1, 4, 3, 2, 1, 2, 1, 2, 2, 1), label = "Would you call yourself a strong [religious preference] or a not very strong [re", format.stata = "%8.0g", labels = c(`Not applicable` = 0,
Strong = 1, `Not very strong` = 2, `Somewhat strong` = 3, `No religion` = 4,
`Don't know` = 8, `No answer` = 9), class = c("haven_labelled",
"vctrs_vctr", "double")), mentloth = structure(c(0, 1, 0, 1,
2, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0
), label = "Should [NAME] go to a therapist, or counselor, like a psychologist, social worke", format.stata = "%8.0g", labels = c(`Not applicable` = 0,
Yes = 1, No = 2, `Don't know` = 8, `No answer` = 9), class = c("haven_labelled",
"vctrs_vctr", "double"))), row.names = c(NA, -25L), class = c("tbl_df",
"tbl", "data.frame"))
任何帮助将不胜感激!
I'm just getting started in R and I'm trying to wrap my head around Chi square for a university assignment.
Specifically, I am using the General Social Survey 2018 dataset (for codebook: https://www.thearda.com/Archive/Files/Codebooks/GSS2018_CB.asp), and I am trying to figure out if religion has any effect on the way people seek out help for mental health.
I want to use reliten
(self-assessment of religiousness - from strong to no religion) as the independent variable, and mentloth
, (asks if a person with mental health issues should reach out to a mental health professional - yes or no) as the dependent variable. Next to the Chi-square, I also want to add CrossTable(GSS18$reliten, GSS18$mentloth)
, but I'm not sure how to take out the "Not applicable", "Don't know" and "No response" values coded as 0, 8 and 9. Anyone has some tips?
Below there is a short preview of my data, if it helps.
structure(list(reliten = structure(c(1, 1, 4, 1, 1, 2, 1, 1,
4, 2, 2, 3, 2, 2, 4, 1, 4, 3, 2, 1, 2, 1, 2, 2, 1), label = "Would you call yourself a strong [religious preference] or a not very strong [re", format.stata = "%8.0g", labels = c(`Not applicable` = 0,
Strong = 1, `Not very strong` = 2, `Somewhat strong` = 3, `No religion` = 4,
`Don't know` = 8, `No answer` = 9), class = c("haven_labelled",
"vctrs_vctr", "double")), mentloth = structure(c(0, 1, 0, 1,
2, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0
), label = "Should [NAME] go to a therapist, or counselor, like a psychologist, social worke", format.stata = "%8.0g", labels = c(`Not applicable` = 0,
Yes = 1, No = 2, `Don't know` = 8, `No answer` = 9), class = c("haven_labelled",
"vctrs_vctr", "double"))), row.names = c(NA, -25L), class = c("tbl_df",
"tbl", "data.frame"))
Any help would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CrossTable 函数来自 gmodels 包,它不知道如何处理 Haven_labelled 类的对象,因此将它们视为数值向量。
为了获得更好的输出,您可以将它们转换为 CrossTable 的基本 R 因子以保留名称。幸运的是,haven 包包含用于执行此操作的函数
as_factor
。完成此操作后,很容易删除不需要的因子水平,如下所示:
所以现在您可以这样做
The
CrossTable
function is from thegmodels
package, which doesn't know how to handle objects of classhaven_labelled
, so treats them as numeric vectors.To get a nicer output, you can convert them into base R factors for
CrossTable
to retain the names. Fortunately, the haven package contains the functionas_factor
for doing exactly that.Once you have done that, it is easy to drop the factor levels you don't want, as shown below:
So now you can do