Dear R users; I would like to convert a series of vectors to matrices in the following way; (2,1,1) to a matrix 1 0 0 1 0 0 0 1 0 0 0 1 The idea is that the column sum of the matrix should be equal to the elements of the vector. Thanks. Vumani
"Vumani Dlamini" <dvumani at hotmail.com> writes:> Dear R users; > > I would like to convert a series of vectors to matrices in the > following way; > (2,1,1) to a matrix > 1 0 0 > 1 0 0 > 0 1 0 > 0 0 1 > > The idea is that the column sum of the matrix should be equal to the > elements of the vector.Didn't we just do that? (No, that was incidence matrices...) i <- rep(1:3,c(2,1,1)) diag(3)[i,] -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Hi, What about: dummy=function(x){ diag(length(x))[rep(1:length(x),x),] } > dummy(c(3,2,4)) [,1] [,2] [,3] [1,] 1 0 0 [2,] 1 0 0 [3,] 1 0 0 [4,] 0 1 0 [5,] 0 1 0 [6,] 0 0 1 [7,] 0 0 1 [8,] 0 0 1 [9,] 0 0 1 HTH, Eric At 12:23 27/05/2004, you wrote:>Dear R users; > >I would like to convert a series of vectors to matrices in the following way; >(2,1,1) to a matrix >1 0 0 >1 0 0 >0 1 0 >0 0 1 > >The idea is that the column sum of the matrix should be equal to the >elements of the vector. > >Thanks. > >VumaniEric Lecoutre UCL / Institut de Statistique Voie du Roman Pays, 20 1348 Louvain-la-Neuve Belgium tel: (+32)(0)10473050 lecoutre at stat.ucl.ac.be http://www.stat.ucl.ac.be/ISpersonnel/lecoutre If the statistics are boring, then you've got the wrong numbers. -Edward Tufte