How to add a column

Asked

Viewed 109 times

2

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$color : Coercing LHS to a list

structure(list(id = c(0L, 1L, 2L, 4L), bone_length = c(0.354512184582154, 
0.575559905025466, 0.467875498742323, 0.776652460655474), rotting_flesh = 
c(0.35083902671065, 
0.425868432210585, 0.354330420391775, 0.508722549943229), hair_length = 
c(0.465760891829121, 
0.531401378709141, 0.811616089668924, 0.636765579959788), has_soul = 
c(0.78114166586219, 
0.439898877037289, 0.791224973337769, 0.884463692097697), color = c(4, 
5, 1, 1), type = structure(c(2L, 3L, 2L, 2L), .Label = c("Ghost", 
"Ghoul", "Goblin"), class = "factor")), .Names = c("id", "bone_length", 
"rotting_flesh", "hair_length", "has_soul", "color", "type"), row.names = 
 c(NA, 
 4L), class = "data.frame")

2 answers

3

First, your question is not reproducible. Next try putting part of the data so we can reproduce the error.

Answer

You can’t use it $ in a Matrix, because a Matrix is just a vector with dimensions. In your case you have to use the function cbind:

train.scale <- cbind(train.scale, newColumn = train$color)

  • Warning message: In cbind(Train.Scale, newColumn = Train$color) : number of Rows of result is not a Multiple of vector length (Arg 2)

  • In the sample I made, I did not get this error. If you want me to reproduce your errors you will have to post part of your data in your question.

  • How do I put the Summary() of the data in the question?

  • or I can put a head(). As you see fit

  • utilize head(train) and then paste the result into the question.

  • 1

    I cannot use how you put it, look at this explanation: https://stackoverflow.com/a/5963610/6532002

  • The best way to post data is dput(head(train, 20)). Post the output of this on question, not in a comment.

Show 3 more comments

3


Thank you for the data format dput.
After viewing the data, the answer is very simple. The correct way to calculate the z-Scores with scale is the following.

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, the sapply.

train.scale2 <- sapply(train[, 2:5], scale)
train.scale2 <- cbind(train.scale2, color = train$color)
  • Now it’s worked out, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.