[R] dummy variables from factors
Noah Silverman
noahsilverman at ucla.edu
Tue Aug 23 20:58:22 CEST 2011
@Josh Thanks! That's exactly what I need.
@ Marc. I want to do this manually because I want to do many things with the data beyond R's built in functions.
--
Noah Silverman
UCLA Department of Statistics
8117 Math Sciences Building
Los Angeles, CA 90095
On Aug 23, 2011, at 10:43 AM, Joshua Wiley wrote:
> On Tue, Aug 23, 2011 at 10:34 AM, Noah Silverman <noahsilverman at ucla.edu> wrote:
>> Hi,
>>
>> Looking at a large data set with many factors.
>>
>> I would like to expand each factor variable into multiple new variables for each level. (0,1) coding.
>>
>> My first though was just to code a big nasty loop, to take each level and cbind a column onto my data set. But, that seems painful. There must be a better way.
>>
>> Is there an "easy" way to do this in R?
>
> Sure, just use model matrix, e.g.:
>
> model.matrix(~ -1 + factor(cyl), data = mtcars)
>
> Cheers,
>
> Josh
>
>>
>> (Note, I don't want to internally represent the levels in the data set, but physically have new columns for each level with a binary indicator.)
>>
>> Thanks!
>>
>> --
>> Noah Silverman
>> UCLA Department of Statistics
>> 8117 Math Sciences Building
>> Los Angeles, CA 90095
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> --
> Joshua Wiley
> Ph.D. Student, Health Psychology
> Programmer Analyst II, ATS Statistical Consulting Group
> University of California, Los Angeles
> https://joshuawiley.com/
More information about the R-help
mailing list