[R] how to combine multiple indicator variables in a single factor

Daniel Nordlund djnordlund at verizon.net
Fri Dec 18 21:48:46 CET 2009


Say I have a dataframe like this:

df <- data.frame(cbind(c(1,0,0,1),c(0,1,0,0),c(0,0,1,0)))

names(df) <- c('a','b','c')

I would like to create a factor in a new column, where the factor values are taken from the column names, like this:

> df2
  a b c f
1 1 0 0 a
2 0 1 0 b
3 0 0 1 c
4 1 0 0 a

How would I do this?  Thanks,

Dan

Daniel Nordlund
Bothell, WA USA




More information about the R-help mailing list