简体   繁体   中英

Using NAs with model.matrix

Let's say I have a variable and I would like to generate dummy variables from it. model.matrix is very helpful

model.matrix(~iris$Species-1)

But let's say I have missing values

iris$Species[1]=NA
model.matrix(~iris$Species-1)

The only difference between these two model.matrix outputs is that the second one omits the row with the NA. I would like model.matrix to treat NA as a unique value, and create an additional column for NAs.

Is there an efficient way of doing this or will I have to write a function to do it?

One solution might be to convert your variable of interest to a factor, and don't exclude NA while doing that:

iris$Species[1] <- NA
mm2 <- model.matrix(~factor(iris$Species, exclude=NULL)-1)
>dim(mm2)
150   4

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