Automatically expanding an R factor into a collection of 1/0 indicator variables for every factor level -


i have r data frame containing factor want "expand" each factor level, there associated column in new data frame, contains 1/0 indicator. e.g., suppose have:

df.original <-data.frame(eggs = c("foo", "foo", "bar", "bar"), ham = c(1,2,3,4)) 

i want:

df.desired  <- data.frame(foo = c(1,1,0,0), bar=c(0,0,1,1), ham=c(1,2,3,4)) 

because analyses need have numeric data frame (e.g., principal component analysis), thought feature might built in. writing function shouldn't hard, can foresee challenges relating column names , if exists already, i'd rather use that.

use model.matrix function:

model.matrix( ~ species - 1, data=iris ) 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -