Hi, I am trying to convert a variable a = sample(1:3,100,rep = T) represents choices into a 3X100 dummy varible b with corresponding element set to 1 otherwise 0. eg. a: 1 3 2 1 2 3 1 1.... b: 1 0 0 1 0 0 1 1.. 0 0 1 0 1 0 0 0... 0 1 0 0 0 1 0 0... Is there something like b[a] =1 existing? I could not figure this out myself. --------------------------------- Mp3·è¿ñËÑ-иèÈȸè¸ßËÙÏ [[alternative HTML version deleted]]
(a==1)*1 or ifelse(a == 1, 1, 0) ------------------------------------------------------------------- Jacques VESLOT CNRS UMR 8090 I.B.L (2?me ?tage) 1 rue du Professeur Calmette B.P. 245 59019 Lille Cedex Tel : 33 (0)3.20.87.10.44 Fax : 33 (0)3.20.87.10.31 http://www-good.ibl.fr ------------------------------------------------------------------- z s a ?crit :> Hi, > > I am trying to convert a variable a = sample(1:3,100,rep = T) represents choices into a 3X100 dummy varible b with corresponding element set to 1 otherwise 0. > eg. > > a: 1 3 2 1 2 3 1 1.... > > b: 1 0 0 1 0 0 1 1.. > 0 0 1 0 1 0 0 0... > 0 1 0 0 0 1 0 0... > > Is there something like b[a] =1 existing? I could not figure this out myself. > > > --------------------------------- > Mp3??????-?????????????? > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
Try using outer: outer(1:3, a, "==")+0 On 8/31/06, z s <flyhyena at yahoo.com.cn> wrote:> Hi, > > I am trying to convert a variable a = sample(1:3,100,rep = T) represents choices into a 3X100 dummy varible b with corresponding element set to 1 otherwise 0. > eg. > > a: 1 3 2 1 2 3 1 1.... > > b: 1 0 0 1 0 0 1 1.. > 0 0 1 0 1 0 0 0... > 0 1 0 0 0 1 0 0... > > Is there something like b[a] =1 existing? I could not figure this out myself. > > > --------------------------------- > Mp3??????-?????????????? > [[alternative HTML version deleted]] > > > > ______________________________________________ > R-help at stat.math.ethz.ch 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. > > >
what about: b <- rbind(1*(a==1),1*(a==2),1*(a==3)) z s wrote:> Hi, > > I am trying to convert a variable a = sample(1:3,100,rep = T) represents choices into a 3X100 dummy varible b with corresponding element set to 1 otherwise 0. > eg. > > a: 1 3 2 1 2 3 1 1.... > > b: 1 0 0 1 0 0 1 1.. > 0 0 1 0 1 0 0 0... > 0 1 0 0 0 1 0 0... > > Is there something like b[a] =1 existing? I could not figure this out myself. > > > --------------------------------- > Mp3??????-?????????????? > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at stat.math.ethz.ch 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.confidentiality notice: The information contained in this e-mail is confidential and...{{dropped}}
Here is an additional way: model.matrix(~ factor(a) - 1) On 9/1/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> Try using outer: > > outer(1:3, a, "==")+0 > > > > > On 8/31/06, z s <flyhyena at yahoo.com.cn> wrote: > > Hi, > > > > I am trying to convert a variable a = sample(1:3,100,rep = T) represents choices into a 3X100 dummy varible b with corresponding element set to 1 otherwise 0. > > eg. > > > > a: 1 3 2 1 2 3 1 1.... > > > > b: 1 0 0 1 0 0 1 1.. > > 0 0 1 0 1 0 0 0... > > 0 1 0 0 0 1 0 0... > > > > Is there something like b[a] =1 existing? I could not figure this out myself. > > > > > > --------------------------------- > > Mp3??????-?????????????? > > [[alternative HTML version deleted]] > > > > > > > > ______________________________________________ > > R-help at stat.math.ethz.ch 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. > > > > > > >
Hallo probably there are other options but outer(1:3,a, "==")*1 can do what you want. HTH Petr On 31 Aug 2006 at 22:41, z s wrote: Date sent: Thu, 31 Aug 2006 22:41:27 +0800 (CST) From: z s <flyhyena at yahoo.com.cn> To: r-help at stat.math.ethz.ch Subject: [R] newbie question about index> Hi, > > I am trying to convert a variable a = sample(1:3,100,rep = T) > represents choices into a 3X100 dummy varible b with corresponding > element set to 1 otherwise 0. > eg. > > a: 1 3 2 1 2 3 1 1.... > > b: 1 0 0 1 0 0 1 1.. > 0 0 1 0 1 0 0 0... > 0 1 0 0 0 1 0 0... > > Is there something like b[a] =1 existing? I could not figure this out > myself. > > > --------------------------------- > Mp3??????-?????????????? > [[alternative HTML version deleted]] > >Petr Pikal petr.pikal at precheza.cz
Thanks for all these replies, all work perfectly. Sun --- Petr Pikal <petr.pikal at precheza.cz>????:> Hallo > > probably there are other options but > > outer(1:3,a, "==")*1 > > can do what you want. > > HTH > Petr > > > > On 31 Aug 2006 at 22:41, z s wrote: > > Date sent: Thu, 31 Aug 2006 22:41:27 +0800 > (CST) > From: z s <flyhyena at yahoo.com.cn> > To: r-help at stat.math.ethz.ch > Subject: [R] newbie question about index > > > Hi, > > > > I am trying to convert a variable a > sample(1:3,100,rep = T) > > represents choices into a 3X100 dummy varible b > with corresponding > > element set to 1 otherwise 0. > > eg. > > > > a: 1 3 2 1 2 3 1 1.... > > > > b: 1 0 0 1 0 0 1 1.. > > 0 0 1 0 1 0 0 0... > > 0 1 0 0 0 1 0 0... > > > > Is there something like b[a] =1 existing? I could > not figure this out > > myself. > > > > > > --------------------------------- > > Mp3??????-?????????????? > > [[alternative HTML version deleted]] > > > > > > Petr Pikal > petr.pikal at precheza.cz > >___________________________________________________________ ????????????????-3.5G??????20M??????