[英]Find all rows of matrix equal to vector
Suppose I have the following matrix: 假设我有以下矩阵:
cm<-structure(c(100, 200, 400, 800, 100, 200, 400, 800, 100, 200,
400, 800, 100, 200, 400, 800, 100, 200, 400, 800, 0, 0, 0, 0,
0.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 0, 0, 0, 0, 0.5, 0.5, 0.5, 0.5,
-0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4, -0.4,
-0.4, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1), .Dim = c(20L, 4L), .Dimnames = list(
NULL, c("Var1", "Var2", "Var3", "n1")))
and another matrix derived from it: 以及从中衍生的另一个矩阵:
a4<-data.matrix(unique(cm[,1:3]))
Now, I want to find all the rows of cm
whose first three columns are equal to a4[1,]
, but doing the intutive thing: 现在,我想找到前三行等于
a4[1,]
的cm
的所有行,但是做了直观的事情:
a5<-which(cm[,1:3]==a4[1,])
fails ( R 3.1.3
). 失败(
R 3.1.3
)。 For example a5[2]
is 13, but the 13th row of cm[,1:3]
ain't the same as a4[1,]
. 例如
a5[2]
为13,但第13行cm[,1:3]
与a4[1,]
。
The function row.match
in prodlim
is easy to use, and ideal for your problem. 该功能
row.match
在prodlim
易于使用,并且非常适合您的问题。
library(prodlim)
row.match(a4[1,], cm[,1:3])
[1] 1
Use apply
and all.equal
to compare each row against the target row. 使用
apply
和all.equal
将每一行与目标行进行比较。 The problem with using ==
is that it only checks the it recycles elements of a vector for comparison, whereas you want to see if all values in the row vector match a4[1,]
so you should use all.equal
. 使用
==
的问题在于它只检查它回收矢量的元素以进行比较,而你想看看行矢量中的所有值是否与a4[1,]
匹配a4[1,]
所以你应该使用all.equal
。 The consequence is that it's return value is not a logical but instead a character string describing differences between the objects, which makes it a little messier to work with than ==
alone: 结果是它的返回值不是逻辑,而是描述对象之间差异的字符串,这使得使用它比单独使用
==
麻烦:
which(apply(cm, 1, function(x) all.equal(x[1:3], a4[1,])) == "TRUE")
# [1] 1
You can also make that a bit simpler by using identical
instead of all.equal
: 你也可以通过使用
identical
而不是all.equal
来all.equal
更简单:
which(apply(cm, 1, function(x) identical(x[1:3], a4[1,])))
# [1] 1
Then extract: 然后提取:
cm[apply(cm, 1, function(x) identical(x[1:3], a4[1,])),,drop=FALSE]
# Var1 Var2 Var3 n1
# [1,] 100 0 -0.4 1
To clarify exactly what's happening, consider what ==
does implicitly when you pass a matrix argument: 为了明确说明发生了什么,请在传递矩阵参数时考虑
==
隐式执行的操作:
which(cm[,1:3]==a4[1,])
# [1] 1 13 23 35 42 45 48 51 53 56 59
That result is the same as converting the matrix to a vector: 该结果与将矩阵转换为向量相同:
as.vector(cm[,1:3])
# [1] 100.0 200.0 400.0 800.0 100.0 200.0 400.0 800.0 100.0 200.0 400.0 800.0 100.0 200.0 400.0 800.0 100.0 200.0 400.0 800.0 0.0 0.0 0.0 0.0 0.5 0.5 0.5
# [28] 0.5 1.0 1.0 1.0 1.0 0.0 0.0 0.0 0.0 0.5 0.5 0.5 0.5 -0.4 -0.4 -0.4 -0.4 -0.4 -0.4 -0.4 -0.4 -0.4 -0.4 -0.4 -0.4 0.0 0.0
# [55] 0.0 0.0 0.0 0.0 0.0 0.0
which(as.vector(cm[,1:3])==a4[1,])
# [1] 1 13 23 35 42 45 48 51 53 56 59
Thus, the positions are positions within the vector representation of cm
, not rows in the matrix representation. 因此,位置是
cm
的向量表示内的位置,而不是矩阵表示中的行。 ==
comparisons can also be dangerous (again do to the recycling noted above) when trying to compare vectors that are not of equivalent length or where one vector's length is not a multiple of the other, which will produce a warning: 当尝试比较长度不等的向量或者一个向量的长度不是另一个向量的倍数时,
==
比较也可能是危险的(再次对上面提到的回收做),这会产生警告:
1:2 == 1:3
# [1] TRUE TRUE FALSE
# Warning message:
# In 1:2 == 1:3 :
# longer object length is not a multiple of shorter object length
Whereas there is no warning when recycling is used: 使用回收时没有警告:
1:2 == 1:6
# [1] TRUE TRUE FALSE FALSE FALSE FALSE
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.