假设我有一个向量:
a<-c(0,0,0,0,1)
我想列出与完全不同一位的所有向量a
。
期望的结果将列出向量的所有1位邻居 a
0,0,0,0,0
1,0,0,0,1
0,1,0,0,1
0,0,1,0,1
0,0,0,1,1
您可以尝试以下方法:
a <- c(0, 0, 0, 0, 1) # your input vector
m <- expand.grid(rep(list(0:1), length(a)) # all combinations of 0/1 of length a
temp <- sapply(seq_along(a), function(i) m[,i] == a[i]) # check the differences
m[rowSums(temp) == (length(a)-1),] # use the index to subset
# Var1 Var2 Var3 Var4 Var5
#1 0 0 0 0 0
#18 1 0 0 0 1
#19 0 1 0 0 1
#21 0 0 1 0 1
#25 0 0 0 1 1
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句