[英]R: finding rows of one matrix in another matrix
我们从:
m1 = matrix(c(1:32), ncol=4, byrow = T); m2 = matrix(c(1:16), ncol=4, byrow=T);
如果不明显,这将产生2个矩阵,一个是8x4,另一个是4x4,使得前者的前4行与后者的前4行相同。
我想要一个带sudo / semi代码的函数;
#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相同的矩阵,除了它有一个0和1的列,表示在M1中找到了M2。
我的真实数据集很大,所以试图找到最佳解决方案。
data.table
是解决此类问题的快速解决方案:
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))]
在这里,我们使用每个的前四列将DT2
LETER DT2
到DT1
。 这会产生:
# 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
转换回矩阵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.