model.matrix() with na.action=NULL?

前端 未结 4 1242
无人共我
无人共我 2020-12-13 13:08

I have a formula and a data frame, and I want to extract the model.matrix(). However, I need the resulting matrix to include the NAs that were found in the orig

4条回答
  •  悲&欢浪女
    2020-12-13 13:34

    Another way is to use the model.frame function with argument na.action=na.pass as your second argument to model.matrix:

    > model.matrix(ff, model.frame(~ ., dat, na.action=na.pass))
       (Intercept)          b fact2 fact3 fact4 fact5
    1            1 -1.3560754     0     0     0     0
    2            1  2.5476965     0     0     0     0
    3            1  0.4635628    NA    NA    NA    NA
    4            1 -0.2871379     1     0     0     0
    5            1  2.2684958     0     1     0     0
    6            1 -1.3560754     0     1     0     0
    7            1  2.5476965     0     0     1     0
    8            1  0.4635628     0     0     1     0
    9            1 -0.2871379     0     0     0     1
    10           1  2.2684958     0     0     0     1
    

    model.frame allows you to set the appropriate action for na.action which is maintained when model.matrix is called.

提交回复
热议问题