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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With