如何解决有没有一种方法可以直接从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"," 5","11","10","14","50","32"," 6","17","13","21","12"," 3"," 4"," 2","15","23","18"," 1","25","16","24"," 8","27","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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。