简体   繁体   中英

One new column gets added when i use model.matrix on a column that has factors

One column x4 in my data set has characters A,B,C. I want to use this model.matrix function on this data set. So i coerce the column that has characters into a factor column.

mydata1$x4 = factor(mydata1$x4,labels = c("1","2","3"))
x=model.matrix(y~.,data=mydata1)[,-1]

But when i use the model.matrix function the output has five columns. The column x4 from the existing data set got split into x42 and x43 columns. Where have i gone wrong? I get the output as shown below.

输出量

When you use mode.matrix you column with Factor data type split into separate columns. so you can change your x4 column data type to number type to prevents of splitting

mydata1$x4 <- as.numeric(mydata1$x4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM