train <- read.csv("train.csv")
train$color <- as.numeric(as.factor(train$color))
train.scale <- scale(train[,2:5])
train.scale$color <- train$color
ERROR:
In train.scale $ color
train <- read.csv("train.csv")
train$color <- as.numeric(as.factor(train$color))
train.scale <- scale(train[,2:5])
train.scale$color <- train$color
ERROR:
In train.scale $ color
Thanks for the data in dput
format.
After seeing the data, the answer is very simple. The correct way to calculate z-scores with scale
is as follows.
train.scale <- as.data.frame(sapply(train[, 2:5], scale))
train.scale$color <- train$color
Note:
The form proposed by Willian Vieira would also work, but if we do not use as.data.frame
, the result is class matrix
. In any case,% w% of% is retained.
train.scale2 <- sapply(train[, 2:5], scale)
train.scale2 <- cbind(train.scale2, color = train$color)
First, your question is not reproducible. At the next try to put some of the data so we can reproduce the error.
Response
You can not use $
in a matrix, because a matrix is just a vector with dimensions. In your case you have to use the cbind
function:
train.scale <- cbind(train.scale, newColumn = train$color)