# first, create your data.frame
mydf <- data.frame(a = c(1,2,3), b = c(1,2,3), c = c(1,2,3))
# then, create your model.matrix
mym <- model.matrix(as.formula("~ a + b + c"), mydf)
# how can I normalize the model.matrix?
Currently, I have to convert my model.matrix back into a data.frame in order to run my normalization function:
normalize <- function(x) { return ((x - min(x)) / (max(x) - min(x))) }
m.norm <- as.data.frame(lapply(m, normalize))
Is there anyway to avoid this step by simply normalizing the model.matrix?
You can normalize each column without converting to a data frame with the
apply
function:You probably actually want to leave the intercept untouched, with something like: