Re: [R] Converting a categorical variable to multiple dichotemousvariables

From: Shane Phillips <SPhillips_at_Lexington1.net>
Date: Tue, 12 Apr 2011 11:04:23 -0400

Here is my original script.

subject=1:1000
treat=rbinom(1*1000,1,.13)
gender=rbinom(1*1000,1,.5)
eth=runif(1*1000, min=1, max=4)
cogat=rnorm(1*1000, 100, 16)
map=rnorm(1*1000, 200, 9)
growth=0
simtest=data.frame (subject=subject, treat=treat, gender=gender, eth=round(eth,digits=0), cogat=round(cogat,digits=0),map=round(map,digits=0),growth) simtest<-transform(simtest, growth=rnorm(1000,m=ifelse(treat==0,0.1,0.5),s=0.03)) simtest

The categorical variable I'm looking at is "eth." I want to change it to a factor. The code you gave works great by itself, but I can't apply it to this program.

Sorry! I'm really new to this.

Thanks for your help!
Shane

-----Original Message-----
From: Dr. Pablo E. Verde [mailto:PabloEmilio.Verde_at_uni-duesseldorf.de] Sent: Tuesday, April 12, 2011 8:53 AM
To: andrija djurovic; Shane Phillips
Cc: r-help_at_r-project.org
Subject: Re: [R] Converting a categorical variable to multiple dichotemousvariables

Hi Shane,

An alternative is:

cat<-as.factor(c(1,1,3,2,4))
outer(cat, levels(cat), "==")+0

Cheers,

Pablo

> hi:
> here is one solution:
>
> cat<-as.factor(c(1,1,3,2,4))
> model.matrix(~cat-1,cat)
>
> cbind(cat,model.matrix(~cat-1,cat))
> Andrija
>
> On Tue, Apr 12, 2011 at 2:17 PM, Shane Phillips
> <SPhillips_at_lexington1.net>wrote:
>
>> I have a categorical variable in a dataframe similar to the following...
>>
>> cat
>> 1
>> 1
>> 3
>> 2
>> 4
>>
>> I need to convert it to 4 dichotemous variables for each observations
>> like...
>>
>> cat1 cat2 cat3 cat4
>> 1 0 0 0
>> 1 0 0 0
>> 0 0 1 0
>> 0 1 0 0
>> 0 0 0 1
>>
>>
>> Thanks in advance!
>>
>> Shane
>>
>> ______________________________________________
>> 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<http://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



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. Received on Tue 12 Apr 2011 - 15:14:03 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 12 Apr 2011 - 16:10:29 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive