有没有一种方法可以直接从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),我得到了以下输出:错误:在chisq.test 中,"x"的所有条目都必须是非负的和有限的

通过阅读类似的问题,我认为你基本上必须在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")))

x应该是一个数字矢量或matrix。这里,它是一个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

最新更新