表函数为我提供了长表格而不是 r 中的宽表格(卡方分析所需)

发布于 2025-01-15 18:40:05 字数 2377 浏览 2 评论 0原文

我正在努力让我的数据集与 r 中的表函数一起使用。我有两个独特的列作为因素,我试图将它们制表为学生种族的一列和他们绘制的种族的九列。我可以手动完成这项工作,但我想要一个列联表来通过卡方分析进行工作。我已经尝试了我能想到的一切,但无法完成这项工作。

每当我使用表格函数时,它看起来像这样:

Predict_Table <- table(Student_Ethnicity, PreDAS_Ethnicity)

Wrong Table

我希望我的数据看起来像这样(但显然有更多列 - 并且它必须能够与一个卡方分析)正确图像

我的数据集 (Predict_DAS)

structure(list(Student_Ethnicity = structure(c(1L, 1L, 1L, 1L, 
2L, 1L, 1L, 3L, 1L, 1L, 4L, 1L, 1L, 4L, 5L, 4L, 1L, 4L, 2L, 5L, 
5L, 2L, 5L, 4L, 4L, 5L, 1L, 1L, 1L, 4L, 4L, 1L, 4L, 5L, 4L, 1L, 
4L, 1L, 4L, 1L, 1L, 6L, 7L, 4L, 1L, 2L, 5L, 6L, 1L, 6L, 6L, 1L, 
5L, 4L, 8L, 1L, 1L, 3L, 1L, 2L, 7L, 4L, 1L, 4L, 8L, 8L, 5L, 1L, 
5L, 5L, 4L, 4L, 4L, 1L, 1L, 4L, 4L, 1L, 4L, 4L, 3L, 1L, 5L, 1L, 
9L, 3L, 1L, 1L, 4L, 1L, 8L, 1L, 1L, 3L, 4L, 1L, 1L, 4L, 7L, 4L, 
3L, 9L, 6L, 1L, 1L, 6L, 4L, 1L, 1L, 1L, 4L, 4L, 5L, 3L, 4L, 4L, 
8L, 1L, 1L, 4L, 1L, 4L, 1L, 2L, 3L, 3L, 3L, 7L, 5L, 1L, 1L, 1L, 
7L, 2L, 1L, 8L, 1L, 5L, 3L, 2L), .Label = c("White/Caucasian", 
"Other", "Multiple", "Black/African American", "Hispanic/Latinx", 
"American Indian or Alaskan Native", "No Selection", "Native Hawaiian or Pacific Islander", 
"Asian"), class = "factor"), PreDAS_Ethnicity = structure(c(1L, 
1L, 9L, 1L, 2L, 7L, 1L, 7L, 7L, 1L, 4L, 1L, 7L, 7L, 5L, 4L, 7L, 
9L, 2L, 7L, 7L, 7L, 1L, 7L, 7L, 5L, 7L, 1L, 7L, 4L, 4L, 1L, 1L, 
5L, 7L, 5L, 7L, 1L, 4L, 1L, 1L, 7L, 9L, 1L, 1L, 7L, 5L, 2L, 1L, 
4L, 7L, 1L, 7L, 4L, 7L, 1L, 9L, 3L, 1L, 1L, 7L, 7L, 7L, 4L, 7L, 
7L, 7L, 1L, 4L, 2L, 4L, 7L, 4L, 1L, 7L, 4L, 7L, 7L, 7L, 4L, 3L, 
4L, 5L, 4L, 9L, 3L, 1L, 1L, 4L, 1L, 7L, 7L, 1L, 7L, 9L, 7L, 1L, 
4L, 7L, 4L, 4L, 4L, 7L, 1L, 7L, 1L, 4L, 1L, 7L, 1L, 7L, 4L, 7L, 
2L, 7L, 7L, 7L, 1L, 1L, 4L, 1L, 4L, 1L, 2L, 1L, 1L, 7L, 7L, 1L, 
7L, 1L, 4L, 4L, 4L, 2L, 7L, 1L, 4L, 7L, 7L), .Label = c("White/Caucasian", 
"Other", "Multiple", "Black/African American", "Hispanic/Latinx", 
"American Indian or Alaskan Native", "No Selection", "Native Hawaiian or Pacific Islander", 
"Asian"), class = "factor")), class = "data.frame", row.names = c(NA, 
-140L))

I'm struggling to get my dataset to work with the table function in r. I have two unique columns as factors that I am trying to tabulate into one column for student ethnicity and nine columns for the ethnicity that they drew. I can manually make this work, but I want a contingency table to work through chi-squared analysis with. I've tried everything I can think of and cannot make this work.

Whenever I have used the table function, it looks like this:

Predict_Table <- table(Student_Ethnicity, PreDAS_Ethnicity)

Wrong Table

I want my data to look like this (but obviously with more columns - and it has to be able to work with a chi-squared analysis):
Correct Image

My Dataset (Predict_DAS):

structure(list(Student_Ethnicity = structure(c(1L, 1L, 1L, 1L, 
2L, 1L, 1L, 3L, 1L, 1L, 4L, 1L, 1L, 4L, 5L, 4L, 1L, 4L, 2L, 5L, 
5L, 2L, 5L, 4L, 4L, 5L, 1L, 1L, 1L, 4L, 4L, 1L, 4L, 5L, 4L, 1L, 
4L, 1L, 4L, 1L, 1L, 6L, 7L, 4L, 1L, 2L, 5L, 6L, 1L, 6L, 6L, 1L, 
5L, 4L, 8L, 1L, 1L, 3L, 1L, 2L, 7L, 4L, 1L, 4L, 8L, 8L, 5L, 1L, 
5L, 5L, 4L, 4L, 4L, 1L, 1L, 4L, 4L, 1L, 4L, 4L, 3L, 1L, 5L, 1L, 
9L, 3L, 1L, 1L, 4L, 1L, 8L, 1L, 1L, 3L, 4L, 1L, 1L, 4L, 7L, 4L, 
3L, 9L, 6L, 1L, 1L, 6L, 4L, 1L, 1L, 1L, 4L, 4L, 5L, 3L, 4L, 4L, 
8L, 1L, 1L, 4L, 1L, 4L, 1L, 2L, 3L, 3L, 3L, 7L, 5L, 1L, 1L, 1L, 
7L, 2L, 1L, 8L, 1L, 5L, 3L, 2L), .Label = c("White/Caucasian", 
"Other", "Multiple", "Black/African American", "Hispanic/Latinx", 
"American Indian or Alaskan Native", "No Selection", "Native Hawaiian or Pacific Islander", 
"Asian"), class = "factor"), PreDAS_Ethnicity = structure(c(1L, 
1L, 9L, 1L, 2L, 7L, 1L, 7L, 7L, 1L, 4L, 1L, 7L, 7L, 5L, 4L, 7L, 
9L, 2L, 7L, 7L, 7L, 1L, 7L, 7L, 5L, 7L, 1L, 7L, 4L, 4L, 1L, 1L, 
5L, 7L, 5L, 7L, 1L, 4L, 1L, 1L, 7L, 9L, 1L, 1L, 7L, 5L, 2L, 1L, 
4L, 7L, 1L, 7L, 4L, 7L, 1L, 9L, 3L, 1L, 1L, 7L, 7L, 7L, 4L, 7L, 
7L, 7L, 1L, 4L, 2L, 4L, 7L, 4L, 1L, 7L, 4L, 7L, 7L, 7L, 4L, 3L, 
4L, 5L, 4L, 9L, 3L, 1L, 1L, 4L, 1L, 7L, 7L, 1L, 7L, 9L, 7L, 1L, 
4L, 7L, 4L, 4L, 4L, 7L, 1L, 7L, 1L, 4L, 1L, 7L, 1L, 7L, 4L, 7L, 
2L, 7L, 7L, 7L, 1L, 1L, 4L, 1L, 4L, 1L, 2L, 1L, 1L, 7L, 7L, 1L, 
7L, 1L, 4L, 4L, 4L, 2L, 7L, 1L, 4L, 7L, 7L), .Label = c("White/Caucasian", 
"Other", "Multiple", "Black/African American", "Hispanic/Latinx", 
"American Indian or Alaskan Native", "No Selection", "Native Hawaiian or Pacific Islander", 
"Asian"), class = "factor")), class = "data.frame", row.names = c(NA, 
-140L))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠 2025-01-22 18:40:05

您的代码将无法工作,因为 R 将无法在数据框中找到变量,例如,

Predict_Table <- table(Student_Ethnicity, PreDAS_Ethnicity)
# Error in table(Student_Ethnicity, PreDAS_Ethnicity) : 
#   object 'Student_Ethnicity' not found

这可以满足您的要求,但类别的顺序不是按字母顺序排列的,因此您必须在创建因子时指定顺序:

Predict_Table <- with(Predict_DAS, table(Student_Ethnicity, PreDAS_Ethnicity))

Your code would not work because R would not be able to find the variables in your data frame, e.g.

Predict_Table <- table(Student_Ethnicity, PreDAS_Ethnicity)
# Error in table(Student_Ethnicity, PreDAS_Ethnicity) : 
#   object 'Student_Ethnicity' not found

This does what you want, but the order of the categories is not alphabetical so you must have specified the order when you created the factors:

Predict_Table <- with(Predict_DAS, table(Student_Ethnicity, PreDAS_Ethnicity))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文