我正在尝试使用以下代码从ISLR扩展Hitters数据:
data = Hitters
apply(data,2,function(x) sum(is.na(x)))
data = subset(data, !is.na(Salary))
apply(data,2,function(x) sum(is.na(x)))
maxs <- apply(data, 2, max)
mins <- apply(data, 2, min)
scaled <- as.data.frame(scale(data, center = mins, scale = maxs - mins))
其中,我从神经网络上的this R-Bloggers教程中提取(我正在尝试使用NN为Hitters数据中的Salary构建预测模型)。但是,我一直收到错误:
> scale(data, center = mins, scale = maxs - mins)
Error in scale.default(data, center = mins, scale = maxs - mins) :
length of 'center' must equal the number of columns of 'x'
我在Stackoverflow上阅读了关于此缩放函数错误here的讨论,但不明白为什么我收到此错误,因为maxs和mins与我的矩阵具有相同的列,即Hitters数据。
答案 0 :(得分:1)
如果您删除非数字列...
data = Hitters
apply(data,2,function(x) sum(is.na(x)))
data = subset(data, !is.na(Salary))
apply(data,2,function(x) sum(is.na(x)))
# add this line to remove non-numeric columns.
data <- data[, sapply(data, is.numeric)]
maxs <- as.numeric(apply(data, 2, max) )
mins <- as.numeric(apply(data, 2, min))
scaled <- as.data.frame(scale(data, center = mins, scale = maxs - mins))