kathystehl
kathystehl

Reputation: 841

Scaling data in R, resulting in error: "length of 'center' must equal the number of columns of 'x'"

I am trying to scale the Hitters data from ISLR using the following code:

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))

Which, I pulled from this R-Bloggers tutorial on neural networks (I am trying to build a predictive model for Salary in the Hitters data using NNs). However, I keep getting the error:

> 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'

I read a discussion of this scale function error here on Stackoverflow, but do not understand why I am receiving this error since maxs and mins have the same columns as my matrix, i.e. the Hitters data.

Upvotes: 1

Views: 4606

Answers (1)

cory
cory

Reputation: 6669

It works if you remove the non-numeric columns...

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))

Upvotes: 1

Related Questions