R 检查两个向量是否为子集

R 检查两个向量是否为子集,r,subset,vector,R,Subset,Vector,我试图检查在{1,…,M}中是否有一个整数I,这样a_I=1和b_I=0,在{1,…,M}中是否有一个整数j,这样b_j=1和a_j=0。如果保持不变,则输出1(不可比较);否则,输出为0(它们是可比的) 我试过这个: a我相信这适用于您描述的条件 incomparable <- function(a,b){ cond1 <- sum(a>b) > 0 # Returns true is if there is at least one case such that a

我试图检查在{1,…,M}中是否有一个整数I,这样a_I=1和b_I=0,在{1,…,M}中是否有一个整数j,这样b_j=1和a_j=0。如果保持不变,则输出1(不可比较);否则,输出为0(它们是可比的)

我试过这个:


a我相信这适用于您描述的条件


incomparable <- function(a,b){
cond1 <- sum(a>b) > 0 # Returns true is if there is at least one case such that a_i is 1 and b_i is 0
cond2 <- sum(b>a) > 0 # True if at least one case such that one case with b_i is 1 and a_i is 0
  cond1*cond2
}

##### Check a case where they are incomparable vectors

a<-c(1,0,0,1,0,0,1,0)
b<-c(1,0,0,1,1,0,0,0)

incomparable(a,b)
[1] 1

##### Check case where a = b
a<-c(1,0,0,1)
b<-c(1,0,0,1)
incomparable(a,b)
[1] 0

#### Case where a \subset b
a<-c(1,0,0,0,1,0,1)
b<-c(1,0,0,1,1,0,1)

incomparable(a,b)
[1] 0

### Case where b \subset a
a<-c(1,1,1,0,1,0)
b<-c(1,1,0,0,1,0)

incomparable(a,b)
[1] 0

不可比0返回true是指至少有一种情况a#i为1,b#i为0
cond2 a)>0#如果至少有一种情况,其中一种情况下b#i为1,a#i为0,则为真
cond1*cond2
}
#####检查它们是不可比较向量的情况

a我相信这适用于您描述的条件


incomparable <- function(a,b){
cond1 <- sum(a>b) > 0 # Returns true is if there is at least one case such that a_i is 1 and b_i is 0
cond2 <- sum(b>a) > 0 # True if at least one case such that one case with b_i is 1 and a_i is 0
  cond1*cond2
}

##### Check a case where they are incomparable vectors

a<-c(1,0,0,1,0,0,1,0)
b<-c(1,0,0,1,1,0,0,0)

incomparable(a,b)
[1] 1

##### Check case where a = b
a<-c(1,0,0,1)
b<-c(1,0,0,1)
incomparable(a,b)
[1] 0

#### Case where a \subset b
a<-c(1,0,0,0,1,0,1)
b<-c(1,0,0,1,1,0,1)

incomparable(a,b)
[1] 0

### Case where b \subset a
a<-c(1,1,1,0,1,0)
b<-c(1,1,0,0,1,0)

incomparable(a,b)
[1] 0

不可比0返回true是指至少有一种情况a#i为1,b#i为0
cond2 a)>0#如果至少有一种情况,其中一种情况下b#i为1,a#i为0,则为真
cond1*cond2
}
#####检查它们是不可比较向量的情况
A.