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
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 <-
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
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
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
5 matches
Mail list logo