R 标识所有值与向量匹配的行号

R 标识所有值与向量匹配的行号,r,R,我有一个矩阵: a<-replicate(3,sample(c(0,1),4, replace=T)) aset.seed(42) a

我有一个矩阵:

a<-replicate(3,sample(c(0,1),4, replace=T))
a
set.seed(42)
a<-重复(3,样本(c(0,1),4,替换=T))
#     [,1] [,2] [,3]
#[1,]    1    1    1
#[2,]    1    1    1
#[3,]    0    1    0
#[4,]    1    0    1
b转换矩阵
#逻辑值被强制为colSums中的整数
其中(colSums(t(a)==b)==3L)
#[1] 3
设置种子(42)
a<-重复(3,样本(c(0,1),4,替换=T))
#     [,1] [,2] [,3]
#[1,]    1    1    1
#[2,]    1    1    1
#[3,]    0    1    0
#[4,]    1    0    1
b转换矩阵
#逻辑值被强制为colSums中的整数
其中(colSums(t(a)==b)==3L)
#[1] 3
设置种子(42)
a<-重复(3,样本(c(0,1),4,替换=T))
#     [,1] [,2] [,3]
#[1,]    1    1    1
#[2,]    1    1    1
#[3,]    0    1    0
#[4,]    1    0    1
b转换矩阵
#逻辑值被强制为colSums中的整数
其中(colSums(t(a)==b)==3L)
#[1] 3
设置种子(42)
a<-重复(3,样本(c(0,1),4,替换=T))
#     [,1] [,2] [,3]
#[1,]    1    1    1
#[2,]    1    1    1
#[3,]    0    1    0
#[4,]    1    0    1
b转换矩阵
#逻辑值被强制为colSums中的整数
其中(colSums(t(a)==b)==3L)
#[1] 3
设置种子(1)
a
set.seed(1)
a
set.seed(1)
a
set.seed(1)
a
set.seed(15)
a
set.seed(15)
a
set.seed(15)
a
set.seed(15)
A.
b<-c(0,1,0)
set.seed(42)
a < -replicate(3, sample(c(0, 1), 4, replace=T))
#     [,1] [,2] [,3]
#[1,]    1    1    1
#[2,]    1    1    1
#[3,]    0    1    0
#[4,]    1    0    1


b <- c(0, 1, 0)

#vector recycling is done in row direction --> transpose the matrix
#logical values get coerced to integers in colSums   
which(colSums(t(a) == b) == 3L)
#[1] 3
set.seed(1)
a <- replicate(3,sample(c(0,1),4, replace=T))
b<-c(0,1,0)
which(apply(a, 1, FUN=function(x) all(x == b)))
# [1] 2
set.seed(15)
a <- replicate(5,sample(c(0,3),6, replace=T))
b<-c(0,1,0)
which(apply(a, 1, FUN=function(x) all(x == b)))