Reputation: 191
Having a matrix A like:
[,1] [,2] [,3] [,4]
[1,] 121 114 117 200
[2,] 312 215 78 211
[3,] 413 121 719 117
[4,] 511 615 428 212
[5,] 616 816 114 223
[6,] 117 428 121 211
and a matrix B like:
[,1] [,2]
[1,] 117 121
I want to get only the rows from A where each row of B exists in rows of A. The result should be:
1. row [1]
2. row [3]
3. row [6]
Upvotes: 1
Views: 546
Reputation: 32558
A[apply(A, 1, function(x) all(B[1,] %in% x)),]
# [,1] [,2] [,3] [,4]
#[1,] 121 114 117 200
#[2,] 413 121 719 117
#[3,] 117 428 121 211
Upvotes: 2
Reputation: 953
I think this is not the most elegant way but it works!
rows<-vector()
j<-1
for(i in 1:nrow(A)){
temp<-A[i,]
aux<- which(B %in% temp)
if(length(aux)!=0){
rows[j]<-i
j<-j+1
}
}
Upvotes: 1