[R] manipulating elements of a vector

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu May 27 12:27:44 CEST 2004


"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




More information about the R-help mailing list