Alexander Shenkin
2010-Nov-17 23:00 UTC
[R] translate vector of numbers to indicies of 0/1 matrix
Hello All, Searched around, haven't found a decent solution. I'd like to translate a vector of numbers to a matrix (or to a list of vectors) such that the vector values would serve as indicies of the 1's in an otherwise-zero-filled matrix (or list of vectors). For example:> a = c(1,3,3,4)# perform operation [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 0 1 0 [3,] 0 0 1 0 [4,] 0 0 0 1 Any help greatly appreciated! thanks, allie
Phil Spector
2010-Nov-17 23:08 UTC
[R] translate vector of numbers to indicies of 0/1 matrix
Alexander - If I understand your problem, I think this function will make the matrix you want: makeyourmatrix = function(vec){ n = length(vec) mat = matrix(0,n,n) mat[cbind(1:n,vec)] = 1 mat }> makeyourmatrix(c(1,3,3,4))[,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 0 1 0 [3,] 0 0 1 0 [4,] 0 0 0 1 - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Wed, 17 Nov 2010, Alexander Shenkin wrote:> Hello All, > > Searched around, haven't found a decent solution. > > I'd like to translate a vector of numbers to a matrix (or to a list of > vectors) such that the vector values would serve as indicies of the 1's > in an otherwise-zero-filled matrix (or list of vectors). For example: > >> a = c(1,3,3,4) > > # perform operation > > [,1] [,2] [,3] [,4] > [1,] 1 0 0 0 > [2,] 0 0 1 0 > [3,] 0 0 1 0 > [4,] 0 0 0 1 > > > Any help greatly appreciated! > > thanks, > allie > > ______________________________________________ > R-help at r-project.org 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. >
David Winsemius
2010-Nov-17 23:13 UTC
[R] translate vector of numbers to indicies of 0/1 matrix
On Nov 17, 2010, at 6:00 PM, Alexander Shenkin wrote:> Hello All, > > Searched around, haven't found a decent solution. > > I'd like to translate a vector of numbers to a matrix (or to a list of > vectors) such that the vector values would serve as indicies of the > 1's > in an otherwise-zero-filled matrix (or list of vectors). For example: > >> a = c(1,3,3,4) > > # perform operation > > [,1] [,2] [,3] [,4] > [1,] 1 0 0 0 > [2,] 0 0 1 0 > [3,] 0 0 1 0 > [4,] 0 0 0 1> cc <- matrix(0, ncol=max(a), nrow=length(b)) > cc[matrix(c(1:4, a), ncol=2)] <- 1 # matrix indexing with two column argument > cc [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 0 1 0 [3,] 0 0 1 0 [4,] 0 0 0 1 -- David Winsemius, MD West Hartford, CT
Gabor Grothendieck
2010-Nov-18 00:07 UTC
[R] translate vector of numbers to indicies of 0/1 matrix
On Wed, Nov 17, 2010 at 6:00 PM, Alexander Shenkin <ashenkin at ufl.edu> wrote:> Hello All, > > Searched around, haven't found a decent solution. > > I'd like to translate a vector of numbers to a matrix (or to a list of > vectors) such that the vector values would serve as indicies of the 1's > in an otherwise-zero-filled matrix (or list of vectors). For example: > >> a = c(1,3,3,4) > > # perform operation > > ? ? [,1] [,2] [,3] [,4] > [1,] ? ?1 ? ?0 ? ?0 ? ?0 > [2,] ? ?0 ? ?0 ? ?1 ? ?0 > [3,] ? ?0 ? ?0 ? ?1 ? ?0 > [4,] ? ?0 ? ?0 ? ?0 ? ?1Try this: replace(diag(a)*0, cbind(seq_along(a), a), 1) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com