r - 有没有办法直接从 excel 表中准备数据以在 R 中执行统计测试?
问题描述
首先,对不起,如果这是一个愚蠢的问题。我对 R 比较陌生。请多多包涵,并向我推荐可以更好地学习的方向。
我从 .csv 文件中导入了以下数据
> View(teste01)
> teste01 <- as.matrix(teste01)
> class(teste01)
[1] "matrix" "array"
> teste01
X1 TOTAL SURVIVOR FATAL
[1,] "<40 years" " 7" " 6" " 1"
[2,] "40-60 years" "28" "17" "11"
[3,] "≥60 years" "39" "13" "25"
[4,] "Female" "38" "17" "19"
[5,] "Male" "36" "19" "16"
[6,] "Previous hospitalisation" "40" "21" "19"
[7,] "Hypertension" "41" "17" "24"
[8,] "Diabetes" "29" "12" "17"
[9,] "Obesity" "19" "10" " 9"
我正在寻找一种对其进行卡方检验的方法,但是通过使用chisq.test(teste01)
,我得到以下输出:错误:'x' 的所有条目在 chisq.test 中必须是非负且有限的
通过阅读类似的问题,我开始认为您基本上必须在 R 上从零开始创建一个矩阵。对吗?如果不是,有没有办法直接使用 .csv 和/或 .xlsx 文件中的数据?在我的例子中,那会怎样?
数据
> dput(teste01)
structure(c("<40 years", "40-60 years", "≥60 years", "Female",
"Male", "Previous hospitalisation", "Hypertension", "Diabetes",
"Obesity", "Past smoking history", "Respiratory diseases", "Cardiovascular disease",
"Gastrointestinal diseases", "Central Nervous System diseases",
"Liver diseases", "Past surgery history", "Chronic heart disease",
"Cancer", "Fatigue", "Fever", "Dyspnoea", "Cough", "Coryza",
"Myalgia", "Chest pain", "Pharyngalgia", "Diarrhoea", "Nausea and Vomit",
"Previously hospitalisation", "Diagnosed sepsis by ICU team",
" 7", "28", "39", "38", "36", "40", "41", "29", "19", " 9", " 9",
" 5", " 5", "11", " 7", "10", "14", " 9", "10", "29", "50", "32",
" 7", "11", " 6", " 5", " 9", " 6", "40", "14", " 6", "17", "13",
"17", "19", "21", "17", "12", "10", " 3", " 4", " 2", " 2", " 4",
" 2", " 4", " 6", " 5", " 4", "15", "23", "18", " 2", " 7", " 3",
" 2", " 2", " 2", "21", " 4", " 1", "11", "25", "19", "16", "19",
"24", "17", " 9", " 6", " 5", " 3", " 3", " 7", " 5", " 6", " 8",
" 4", " 6", "14", "27", "14", " 5", " 4", " 3", " 3", " 7", " 4",
"19", "10"), .Dim = c(30L, 4L), .Dimnames = list(NULL, c("X1",
"TOTAL", "SURVIVOR", "FATAL")))
解决方案
THex
应该是一个数字向量或matrix
. 在这里,它是 a matrix
,但它是一个character
矩阵,因为第一列是character
。我们可以使用第一列的行名属性使矩阵数字化。现在,chisq.test
应该工作
teste02 <- teste01[,-1]
teste02[] <- as.numeric(teste02)
class(teste02) <- 'numeric'
row.names(teste02) <- teste01[,1]
chisq.test(teste02)
#Pearson's Chi-squared test
#data: teste02
#X-squared = 23.115, df = 58, p-value = 1
推荐阅读
- java - Opendaylight Sodium 的应用程序构建错误
- python - Matplotlib 底图:地图坐标
- c# - @Html.EditorFor 定位错误的属性
- java - 导入的 jar 在默认包中可见,但在其他包中不可见
- c++ - 在一个类中声明一个指针,但在另一个线程中初始化
- c# - 单元测试命令处理程序依赖于接口
- google-cloud-platform - GCP 自定义角色创建失败,代码为 400
- laravel - laravel 中的多重身份验证
- reactjs - 需要帮助:VS 代码自动完成在 React 中不起作用
- node.js - 节点错误:发送到客户端后无法设置标头