Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting matrix rows conditional on their rowsum?

Tags:

r

matrix

In a matrix, how do I determine the rows that have the largest rowsums. For example, in the following matrix:

     -  A  P  S  T
  -  1  0  0  0  0
  A  0  0  0  0  1
  C  0  0  0  1  0
  P  0  2  0  2  0
  S  0  0  0 23  3
  T  0  0  1  0  0

rows S & P have the two largest rowsums.

like image 663
Ram Avatar asked Dec 07 '25 04:12

Ram


2 Answers

There's no need to use the names, you could easily do :

> Rsum <- rowSums(mat)
>  mat[tail(order(Rsum),2),]
  - A P  S T
P 0 2 0  2 0
S 0 0 0 23 3
like image 87
Joris Meys Avatar answered Dec 08 '25 16:12

Joris Meys


You could do this:

# Build your example matrix
mat = matrix( data=c( 1,0,0,0,0, 0,0,0,0,1, 0,0,0,1,0, 0,2,0,2,0, 0,0,0,23,3, 0,0,1,0,0 ), ncol=5, byrow=T )
rownames( mat ) = c( '-', 'A', 'C', 'P', 'S', 'T' )
colnames( mat ) = c( '-', 'A', 'P', 'S', 'T' )

# Get the sums
sums = rowSums( mat )

# Get the top 2 row names
top.names = names( sums[ order( sums, decreasing=TRUE ) ][ 1:2 ] )

# Filter the original matrix to include just these two
mat[ rownames( mat ) %in% top.names, ]

Which outputs

  - A P  S T
P 0 2 0  2 0
S 0 0 0 23 3
like image 31
tim_yates Avatar answered Dec 08 '25 16:12

tim_yates



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!