R中带置换的置换与规则

R中带置换的置换与规则,r,iterator,permutation,R,Iterator,Permutation,我需要做一种真值表,用于蒙特卡罗模拟 基本上,我需要为0和1生成具有110个“操作数”的排列,但需要一些规则 它不能有两个零并排 使用3列的示例(我需要110) 我试图使用包安排,但我不知道如何应用规则。创建测试数据,g,然后将列粘贴在一起,并使用grep查找没有双零的行。然后用这个索引g g <- expand.grid(c_1 = 0:1, c_2 = 0:1, c_3 = 0:1) # test data ok <- ! grepl("00", do.ca

我需要做一种真值表,用于蒙特卡罗模拟

基本上,我需要为0和1生成具有110个“操作数”的排列,但需要一些规则

它不能有两个零并排

使用3列的示例(我需要110)


我试图使用包安排,但我不知道如何应用规则。

创建测试数据,
g
,然后将列粘贴在一起,并使用
grep
查找没有双零的行。然后用这个索引
g

g <- expand.grid(c_1 = 0:1, c_2 = 0:1, c_3 = 0:1) # test data

ok <- ! grepl("00", do.call("paste0", g))
g[ok, ]
一排 如果有一行作为向量,则在其上使用
as.list
,以使用上述内容:

x <- c(0, 1, 1)
! grepl("00", do.call("paste0", as.list(x)))
## [1] TRUE

x主要问题是110列无法在内存中存储数据。它需要迭代。我认为它应该可以工作,问题是行数。2^110=1.298E33增加了有关枚举和生成随机行的部分;但是,最后一个要求输入零的数量。
  c_1 c_2 c_3
3   0   1   0
4   1   1   0
6   1   0   1
7   0   1   1
8   1   1   1
x <- c(0, 1, 1)
! grepl("00", do.call("paste0", as.list(x)))
## [1] TRUE
library(arrangements)

NotDoubleZero <- function(n, k = 0) {
  structure(NA, icomb = icombinations(n, k))
}

iter <- function(x, ...)  {
  if (is.null(x)) return(NULL)
  icomb <- attr(x, "icomb")
  n <- icomb$n
  k <- icomb$k
  repeat {
    r <- icomb$getnext()
    if (is.null(r) || all(diff(r) > 1)) break
  }
  if (is.null(r)) {
    k <- k + 1
    if (k > ceiling(n/2)) return(NULL)
    x <- NotDoubleZero(n, k)
    iter(x)
  } else structure(r, icomb = icomb)
}

# test
res <- NotDoubleZero(3)
while(!is.null(res <- iter(res))) cat("Positions of 0's: <", res, "> \n")
Positions of 0's: <  >   <- no zeros, i.e. all ones
Positions of 0's: < 1 > 
Positions of 0's: < 2 > 
Positions of 0's: < 3 > 
Positions of 0's: < 1 3 > 
set.seed(123)
n <- 3
k <- 2
repeat {
  r <- sort(sample(n, k))
  if (all(diff(r) > 1)) break
}
r
## [1] 1 3

1 - replace(numeric(n), r, 1)
## [1] 0 1 0