一种方法是
m1 <- apply(ar1, 2, `[`)
m1[m1[,2]%in% 1:2 & m1[,3]==3 & m1[,4]==4,]
# [,1] [,2] [,3] [,4]
#[1,] 1 1 3 4
#[2,] 1 1 3 4
#[3,] 1 2 3 4
#[4,] 1 1 3 4
#[5,] 1 1 3 4
#[6,] 1 2 3 4
或者
res <- do.call(rbind,lapply(seq(dim(ar1)[3]), function(i) {
x1 <- ar1[,,i]
x2 <- t(x1[,-1])
x1[colSums(x2==c(1,3,4)|x2==c(2,3,4))==3,]}))
res
# [,1] [,2] [,3] [,4]
#[1,] 1 1 3 4
#[2,] 1 1 3 4
#[3,] 1 2 3 4
#[4,] 1 1 3 4
#[5,] 1 1 3 4
#[6,] 1 2 3 4
更新
假设values 到match 是否在matrix 中,每一行作为匹配向量。
toMatch <- rbind(c(1,3,4), c(2,3,4), c(4,3,2), c(1,9,4))
indx1 <- apply(toMatch, 1, paste, collapse="")
res <- do.call(rbind,lapply(seq(dim(ar1)[3]), function(i) {
x1 <- ar1[,,i]
x1[apply(x1[,-1], 1, paste, collapse='') %in% indx1,]
}))
数据
ar1 <- structure(c(1, 1, 1, 1, 1, 1, 3, 1, 3, 2, 3, 4, 3, 4, 3, 4, 5,
4, 4, 4, 1, 1, 1, 1, 1, 1, 3, 1, 3, 2, 3, 4, 3, 4, 3, 4, 5, 4,
4, 4), .Dim = c(5L, 4L, 2L))