I have a matrix and I want to change the fifth column type from character into numeric but I can not. I have tried this:
test1[,5] <- as.numeric(test1[,5])
but again the column class is character not numeric. What should I do?
Thank you
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Like @Marius said, a matrix can only hold one data type. You could convert your matrix into a data.frame since data.frames can hold a different data type for each of their columns. The functions for converting from and back are
as.data.frameandas.matrix. You’ll then be able to apply the column conversion code you posted to a data.frame.However, you mentioned in a comment that your ultimate goal was to reorder your matrix based on the values of a coerced column. You don’t need to coerce the column in-place before reordering your matrix, you can do all that on the fly with: