R:在另一个矩阵中查找一个矩阵的行 [英] R: finding rows of one matrix in another matrix

查看:255
本文介绍了R:在另一个矩阵中查找一个矩阵的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们从开始:

m1 = matrix(c(1:32), ncol=4, byrow = T); m2 = matrix(c(1:16), ncol=4, byrow=T);

如果不是很明显,它将构成2个矩阵,一个矩阵为8x4,另一个矩阵为4x4,这样前者的前4行与后者相同.

if it is not obvious, this will make 2 matrices, one is 8x4 the other is 4x4 such that the first 4 rows of the former are identical to that of the latter.

我想要一个下面带有sudo/semi代码的函数;

I want a function with sudo/semi code below;

#x is always the bigger; an if check can be put here but assume nrow(x) > nrow(y)
countAinB<-function(x, y){

#new matrix of 0s that has the same dim of x, add 1 extra column for adding found/not found (0/1) coding
c <-matrix(0, ncol(x)+1, nrow(x))


#need change of for, it is slow in R
for (i in 1:nrow(y)){
    #bad R below
    if(y[i,] in x){
    ??add a 1 to the column matching the found row of y in x to c
}}
return(c)
}
C <- countAinB(M1,M2)

现在C是一个与X相同的矩阵,不同之处在于它的列0s和1s表示在M1中找到了M2.

Now C, is a matrix identical to X, except it has a column of 0s and 1s indicating M2 was found in M1.

我的真实数据集非常庞大,因此尝试寻找最佳解决方案.

My real datasets are huge, so trying to find best solution.

推荐答案

data.table是针对此类问题的快速解决方案:

data.table is a fast solution for this type of problem:

library(data.table)
DT1 <- data.table(m1)
DT2 <- data.table(cbind(m2, 0), key=paste0("V", seq(len=ncol(m2))))
setnames(DT2, c(head(names(DT2), -1L), "found"))
DT2[DT1, list(found=ifelse(is.na(found), 0, 1))]

在这里,我们使用每列的前四列将DT2DT1左连接.这将产生:

Here, we are LEFT JOINING DT2 to DT1 using the first four columns of each. This produces:

#    V1 V2 V3 V4 found
# 1:  1  2  3  4     1
# 2:  5  6  7  8     1
# 3:  9 10 11 12     1
# 4: 13 14 15 16     1
# 5: 17 18 19 20     0
# 6: 21 22 23 24     0
# 7: 25 26 27 28     0
# 8: 29 30 31 32     0

其中found表示该行是否同时存在于两个对象中.您可以使用as.matrix转换回矩阵.

Where found indicates whether the row was present in both objects. You can convert back to matrix with as.matrix.

这篇关于R:在另一个矩阵中查找一个矩阵的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆