表函数为我提供了长表格而不是 r 中的宽表格(卡方分析所需)
我正在努力让我的数据集与 r 中的表函数一起使用。我有两个独特的列作为因素,我试图将它们制表为学生种族的一列和他们绘制的种族的九列。我可以手动完成这项工作,但我想要一个列联表来通过卡方分析进行工作。我已经尝试了我能想到的一切,但无法完成这项工作。
每当我使用表格函数时,它看起来像这样:
Predict_Table <- table(Student_Ethnicity, PreDAS_Ethnicity)
我希望我的数据看起来像这样(但显然有更多列 - 并且它必须能够与一个卡方分析):
我的数据集 (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)
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):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码将无法工作,因为 R 将无法在数据框中找到变量,例如,
这可以满足您的要求,但类别的顺序不是按字母顺序排列的,因此您必须在创建因子时指定顺序:
Your code would not work because R would not be able to find the variables in your data frame, e.g.
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: