首页 > 解决方案 > 为什么我的数字矩阵被转换为字符矩阵?

问题描述

我正在尝试使用二元逻辑回归和惩罚 LASSO 构建预测分类模型,最终我将比较这两个模型。问题是我试图在应用模型(例如多重共线性测试)之前了解更多数据并运行一些测试,但数据类型转换不正确。

数据集由数值变量和因子变量组成。我已经从 csv 文件中导入了 r 中的数据,在导入数据之前,我已经手动将所有“因素”变量更改为“数字”。我已经从整个数据集中专门选择了我想要的列,但是当这样做时,矩阵应该是使用 as.matrix 的数字,但事实并非如此。

Data<- read.csv("Test.csv")
names(Data)
attach(Data)
dim(Data)
sapply(Data,class)
ChurnFlag <- ifelse(ChurnedFlag=="Y",1,0)

#combinding all the new created variables
DataMat <- as.matrix(cbind(Data,ChurnFlag))

#selecting specifically which variables I want to analyse which are all 
numeric/integer
DataMatRed <- as.matrix((DataMat[,c(4:8,10:73,92)]))

DataMatRedNum <- mapply(DataMatRed,FUN=as.numeric) 
#defining the matrix as numeric
is.numeric(DataMatRedNum) #checking that it is numeric

DataMatDF <- as.data.frame(DataMatRed)
DataMatDF2 <- data.frame(DataMatRed,row.names = NULL,check.rows = FALSE,check.names = TRUE) /*

我希望数字矩阵不是字符,因为当尝试colldiag在 R 中运行该函数时它不起作用并且错误如下:

svd(X) 中的错误:“x”中的值无限或缺失

我检查了我是否有任何缺失值并且没有缺失值

标签: rdataframe

解决方案


推荐阅读