如何强制model.matrix()函数将NA视为一个级别?

时间:2017-08-22 05:40:38

标签: r

如何强制model.matrix()函数将NA视为一个组的级别,就像"黄色"例如,不要删除该记录? 我的代码:

 a    <- c(4,NA, 8, 7, 12, 6, 9, 10, 14, 7,9) 
 b  <- as.factor(c(1, 0, 1,NA, 1, 1, 0, 1,1, 0, 0))
 c  <- c(0.86, 0.45, 0.99, 0.84, 0.85, 0.67, 0.91, 0.29, 0.88,1,0.5) 
 d   <- as.factor(c(0, 1, 1, 2, 2, 3, 2, 0, NA,NA,NA))
 e  <- as.factor(c(0, 2, 2, 2, 2, 3, 2, 0, 0 , 2,0))
 f <- as.factor(c("blue", "blue", "yellow", "red", "red", "yellow", 
                    "yellow", "red", "yellow" , "blue" , "red"))
 g <- c(1, 1, 0, 1, 0, 0, 0, 1, 1 , 0,0)

 mat <- model.matrix(g ~ b + d + e + f)[, -1]
 mat #reduce the number of rows containing NA

非常感谢任何帮助。

0 个答案:

没有答案