开发者

Extracting matrix rows conditional on their rowsum?

In a matrix, how do I determin开发者_StackOverflow中文版e 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


Paste your matrix in an easily reproducible format for others checking your question:

m <- structure(list(X. = c(1L, 0L, 0L, 0L, 0L, 0L), A = c(0L, 0L, 
0L, 2L, 0L, 0L), P = c(0L, 0L, 0L, 0L, 0L, 1L), S = c(0L, 0L, 
1L, 2L, 23L, 0L), T = c(0L, 1L, 0L, 0L, 3L, 0L)), .Names = c("X.", 
"A", "P", "S", "T"), class = "data.frame", row.names = c("-", 
"A", "C", "P", "S", "T"))

You could get it with dput, e.g.: dput(YourMatrix). This could be useful for your future questions :)

Back to the question - sort the rowSums and get the names, via:

t <- names(sort(rowSums(m)))

Get the first two:

> t[(length(t)-1):length(t)]
[1] "T" "S"

Or get the wanted rows by:

> d[t[(length(t)-1):length(t)],]
  T  S
- 0  0
A 1  0
C 0  1
P 0  2
S 3 23
T 0  0
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜