[英]Why am I getting Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric
My code is as follows:我的代码如下:
my_filtered_data <- my_data[, colSums(my_data != 0) >= 300]
set.seed(123)
data1.csv <- my_filtered_data[sample(nrow(my_filtered_data), 200), ]
data2.csv <- data.frame(data1.csv)
data3.csv <- scale(data2.csv, center = TRUE) # Gives error.
Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric
colMeans(x, na.rm = TRUE) 中的错误:“x”必须是数字
Can someone explain why I am receiving this error?有人可以解释为什么我收到此错误吗?
This is a bit longer for a comment, may or may not answer the problem.这对于评论来说有点长,可能会也可能不会回答问题。 But I think this can be one of the issues with OP dataset.
但我认为这可能是 OP 数据集的问题之一。
You used data.frame command , now data.frame contains a parameter stringsAsFactors = TRUE
by default,and probably this is converting one of your columns to factor, that is why you are getting this error, One way to avoid it to use options(stringsAsFactors=FALSE)
on top of your code or use data.frame(your_object, stringsAsFactors=FALSE)
您使用了 data.frame 命令,现在 data.frame 默认包含一个参数
stringsAsFactors = TRUE
,可能这是将您的一列转换为因子,这就是您收到此错误的原因,避免它使用options(stringsAsFactors=FALSE)
一种方法options(stringsAsFactors=FALSE)
在您的代码之上或使用data.frame(your_object, stringsAsFactors=FALSE)
Just to recreate the error , you can use iris dataset to display a similar error,只是为了重新创建错误,您可以使用 iris 数据集来显示类似的错误,
scale(iris[,1:5], center=TRUE, scale=TRUE)
## This fails with the same error as the last column in iris data set is a factor
but this will work,但这会奏效,
scale(iris[,1:4], center=TRUE, scale=TRUE)
Note I am dropping here the column, In your case you might want to change it to numeric(so it totally depends on what you are trying to do here).请注意,我将列放在此处,在您的情况下,您可能希望将其更改为数字(因此这完全取决于您在此处尝试执行的操作)。 In case you do want to change it to numeric from a factor, try running
as.numeric(as.character(your_column))
.如果您确实想将其从因子更改为数字,请尝试运行
as.numeric(as.character(your_column))
。
Also as suggested in comments , try avoiding names which contains dot in in your object in R.同样如评论中所建议的那样,尝试避免在 R 中的对象中使用包含点的名称。
So, sum of all the info can be wrapped in one line that , you should try this:因此,所有信息的总和可以包含在一行中,您应该尝试以下操作:
data.frame(data1.csv, stringsAsFactors=FALSE)
then try running the scale
command. data.frame(data1.csv, stringsAsFactors=FALSE)
然后尝试运行scale
命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.