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

From: Jeremy Hetzel <>
Date: Tue, 12 Apr 2011 08:53:25 -0700 (PDT)


Does this work?

# Your simulated data

eth=runif(1*1000, min=1, max=4)
cogat=rnorm(1*1000, 100, 16)
map=rnorm(1*1000, 200, 9)
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)) head(simtest)

# From Pablo above, but with unique() instead of levels(), since your eth is
numeric, not a factor
simtest.dichotomous <- outer(simtest$eth, unique(simtest$eth), "==") + 0

# Combine

simtest.combined <- cbind(simtest, simtest.dichotomous)



Jeremy Hetzel
Boston University mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 12 Apr 2011 - 16:02:41 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:30 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive