[R] how to generate matrices based on an indicator variable

From: Ya-Hsiu Chuang <amichuang_at_hotmail.com>
Date: Wed 04 Oct 2006 - 05:13:56 GMT


Hi,

I am trying to create matrices X's based on one indicator variable, r.

Given a 4 x4 matrix X= [ 0,0,0,0

                                        0,0,0,0
                                        0,0,0,0
                                        0,0,0,1]
r= c(0, 0, 0, 1)

I'd like to create separate matrices where when i = 1, X= [1,0,0,0

                          0,0,0,0
                          0,0,0,0
                          0,0,0,1]

when i =2 , X= [0,0,0,0
                         0,1,0,0
                         0,0,0,0
                         0,0,0,1]

when i = 3, X=  [0,0,0,0
                          0,0,0,0
                          0,0,1,0
                          0,0,0,1]

when i = 4, X=  [0,0,0,0 
                          0,0,0,0
                          0,0,0,0
                          0,0,0,1]

Any suggestions would be very appreciated.

Thanks

        [[alternative HTML version deleted]]



R-help@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. Received on Wed Oct 04 23:21:17 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 04 Oct 2006 - 14:30:11 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.