Skip to content
Prev 326224 / 398502 Next

subset of factors in a regression

Philip A. Viton <viton.1 <at> osu.edu> writes:
You could use model.matrix(~state-1) and select the columns
you want, e.g.

state <- state.abb; m <- model.matrix(~state-1)
m[,colnames(m) %in% c("stateCA","stateMA")]

 -- but this will actually create a bunch of vectors you
want before throwing them away.

more compactly:

m <- sapply(cstates,"==",state)
storage.mode(m) <- "numeric"
## or m[] <- as.numeric(m)