简体   繁体   English

cor(mydata)中的错误:'x'必须是数字

[英]Error in cor(mydata) : 'x' must be numeric

In R, I have been having trouble trying to create a correlation matrix for my data.在 R 中,我在尝试为我的数据创建相关矩阵时遇到了麻烦。 I keep running into this problem: "Error in cor(mydata): 'x' must be numeric" and I don't know how to fix it.我一直遇到这个问题:“cor(mydata) 中的错误:'x' 必须是数字”,我不知道如何解决它。

> mydata <- Combo[, c(1,2,3,4,5,6,7)]
> head(mydata, 13)

> #computing matrix
> corrmax = cor(mydata)
**Error in cor(mydata) : 'x' must be numeric**
> 

在此处输入图像描述

I believe not all the data in mydata are numeric.我相信并非mydata中的所有数据都是数字的。 You can test this by running: str(mydata) or sapply(mydata, is.numeric) .您可以通过运行来测试它: str(mydata)sapply(mydata, is.numeric)

If there are variables in mydata that are chr or other non-numeric formats or return FALSE in the case of sapply , you will need to convert them to numeric before running the command or be more selective about the set of variables for which you calculate a correlation.如果mydata中存在chr或其他非数字格式的变量,或者在sapply的情况下返回FALSE ,则需要在运行命令之前将它们转换为数字,或者对计算 a 的变量集更具选择性相关性。 I see strings and percent signs in what you posted.我在您发布的内容中看到了字符串和百分号。 The strings will need to be removed and the formatted percents (%) converted to a numeric representation (decimals).需要删除字符串并将格式化的百分比 (%) 转换为数字表示(小数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM