Re: [R] Matrix transformation problem

2008-06-11 Thread Berend Hasselman
stefan.petersson wrote: > > > ng, > > I have a matrix (x) with binary content. Each row of the matrix holds > exactly one 1, and the rest of the row is zeros. The thing is that I need > to 'collapse' the matrix to one column where each row holds the original > column index of the 1's (y). Som

Re: [R] Matrix transformation problem

2008-06-11 Thread Ted Harding
For precisely this particular type of question, the following seems to be the simplest, most direct, and most transparent solution: rowSums(x%*%(1:ncol(x))) # [1] 1 3 2 3 2 1 Ted. On 11-Jun-08 09:21:35, Dimitris Rizopoulos wrote: > sorry, my previous answer was not correct; you need: > > x <-

Re: [R] Matrix transformation problem

2008-06-11 Thread Eik Vettorazzi
you may try a matrix multiplication, which has a very high performance in R x%*%1:ncol(x) hth. [EMAIL PROTECTED] schrieb: ng, I have a matrix (x) with binary content. Each row of the matrix holds exactly one 1, and the rest of the row is zeros. The thing is that I need to 'collapse' the ma

Re: [R] Matrix transformation problem

2008-06-11 Thread Dimitris Rizopoulos
sorry, my previous answer was not correct; you need: x <- matrix(c(1,0,0, 0,0,1, 0,1,0, 0,0,1, 0,1,0, 1,0,0), ncol = 3, byrow = TRUE) which(t(x == 1), arr.ind = TRUE)[, "row", drop = FALSE] Best, Dimitris Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic Univer

Re: [R] Matrix transformation problem

2008-06-11 Thread Dimitris Rizopoulos
try this: x <- matrix(c(1,0,0, 0,0,1, 0,1,0, 0,0,1, 0,1,0, 1,0,0), ncol = 3, byrow = TRUE) which(x == 1, arr.ind = TRUE)[, "col", drop = FALSE] I hope it helps. Best, Dimitris Dimitris Rizopoulos Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kap