Friday, 15 August 2014

r - Convert a factor to indicator variables? -



r - Convert a factor to indicator variables? -

this question has reply here:

automatically expanding r factor collection of 1/0 indicator variables every factor level 7 answers

how convert factor in r several indicator variables, 1 each level?

one way utilize model.matrix():

model.matrix(~species, iris) (intercept) speciesversicolor speciesvirginica 1 1 0 0 2 1 0 0 3 1 0 0

....

148 1 0 1 149 1 0 1 150 1 0 1 attr(,"assign") [1] 0 1 1 attr(,"contrasts") attr(,"contrasts")$species [1] "contr.treatment"

r

No comments:

Post a Comment