在R中,从矩阵中提取由向量的组成部分索引的所有行 [英] In R, Extract from a matrix all rows indexed by the components of a vector
本文介绍了在R中,从矩阵中提取由向量的组成部分索引的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在R中,令M为矩阵:
[,1] [,2]
[1,] 1 9
[2,] 3 12
[3,] 6 4
[4,] 7 2
我想提取所有条目,其条目等于向量的分量
M中的[,1]列中的v <- c(3,6,1)
产生子矩阵m:
I would like to extract all rows with entries equal to the components of the vector
v <- c(3,6,1)
from column [,1] in M producing the submatrix m:
[,1] [,2]
[1,] 1 9
[2,] 3 12
[3,] 6 4
我尝试过
m <- M[which(M[,1] == v), ]
获取错误消息longer object length is not a multiple of shorter object length
.
使用v
的转置t(v)
没有帮助.
obtaining the error message longer object length is not a multiple of shorter object length
.
Using the transpose t(v)
of v
does not help.
非常感谢!
Avitus
推荐答案
使用%in%
:
M[M[,1] %in% v,]
[,1] [,2]
[1,] 1 9
[2,] 3 12
[3,] 6 4
这篇关于在R中,从矩阵中提取由向量的组成部分索引的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文