根据行和提取矩阵行?

发布于 2024-10-25 00:38:49 字数 225 浏览 4 评论 0原文

在矩阵中,如何确定具有最大行总和的行。例如,在以下矩阵中:

     -  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

行S & P 具有两个最大的行和。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南巷近海 2024-11-01 00:38:49

无需使用名称,您可以轻松做到:

> Rsum <- rowSums(mat)
>  mat[tail(order(Rsum),2),]
  - A P  S T
P 0 2 0  2 0
S 0 0 0 23 3

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
淡莣 2024-11-01 00:38:49

你可以这样做:

# 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, ]

哪个输出

  - 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
孤君无依 2024-11-01 00:38:49

以易于复制的格式粘贴您的矩阵,以便其他人检查您的问题:

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"))

您可以使用 dput 获取它,例如:dput(YourMatrix)。这可能对您将来的问题很有用:)

回到问题 - 对 rowSums 进行排序并获取名称,通过:

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

获取前两个:

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

或通过以下方式获取所需的行:

> 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文