Reputation: 51
I have the matrix below:
x=
1 0 1 1
0 1 0 0
1 0 0 0
I need to retrieve the following values (corresponding to column indices):
In the first row, r1= "1","3" and "4".
In the second row, r2= "2"
In the third row, r3= "1"
My real data is a matrix of 300X66,000.
Upvotes: 2
Views: 98
Reputation: 886948
We could create a logical index and use which
with arr.ind=TRUE
to get row/column index. It may be better to store it as such. We could also split
the 'indx' to a 'list'.
indx <- which(X!=0, arr.ind=TRUE)
split(indx[,2], indx[,1])
#$`1`
#[1] 1 3 4
#$`2`
#[1] 2
#$`3`
#[1] 1
Or use apply
with MARGIN=1
apply(!!X, 1, which)
Upvotes: 2