Hello, All:

      What's the simplest way to convert a data.frame into a model.matrix?


One way is given by the following example, modified from the examples in help(model.matrix):


dd <- data.frame(a = gl(3,4), b = gl(4,1,12))
ab <- model.matrix(~ a + b, dd)
ab0 <- model.matrix(~., dd)
all.equal(ab, ab0)


What do you think about replacing "model.matrix(~ a + b, dd)" in the current help(model.matrix) with this 3-line expansion?


I suggest this, because I spent a few hours today trying to convert a data.frame into a model.matrix before finding this.


Also, what do you think about adding something like the following to the stats package:


model.matrix.data.frame <- function(object, ...){
    model.matrix(~., object, ...)
}


      And then extend the above example as follows:

ab. <- model.matrix(dd)
all.equal(ab, ab.)


      Thanks,
      Spencer Graves

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to