Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 采样并允许重复,但不允许在一行中出现两次_R - Fatal编程技术网

R 采样并允许重复,但不允许在一行中出现两次

R 采样并允许重复,但不允许在一行中出现两次,r,R,给定4个数字对象,例如 df<-data.frame(a=1:5, b=6:10, c=11:15, d=16:20) 好: 非常手动地,您可以测试每个数字是否等于所有其他数字,测试一行中是否有两个TRUE,并在此基础上重复采样,直到条件为FALSE 您可以在这样的函数中实现 sampler <- function(number_sample) { x <- sample(1:number_sample, replace = TRUE) pre_test <-

给定4个数字对象,例如

df<-data.frame(a=1:5, b=6:10, c=11:15, d=16:20)
好:


非常手动地,您可以测试每个数字是否等于所有其他数字,测试一行中是否有两个
TRUE
,并在此基础上重复采样,直到条件为
FALSE

您可以在这样的函数中实现

sampler <- function(number_sample) {
  x <- sample(1:number_sample, replace = TRUE)
  pre_test <- lapply(x, function(single_number) diff(which(single_number == x)) == 1)
  test <- any(sapply(pre_test, any))

  if (test) sampler(number_sample) else x
}

sampler(100)
sampler(10)

sampler基于sample()的替代函数


custom.sampling您可以这样做

values <- 1:4 #values to sample from
len <- 20 #number of samples
samp <- sample(values,1) #initialise variable
length(samp) <- len
sapply(2:len, function(i) samp[i] <<- sample(setdiff(values, samp[i-1]), 1))

samp
[1] 2 1 4 1 4 3 2 4 3 1 3 1 4 3 4 1 3 1 4 2

值不进行拒绝采样。这使用table()来快速检查和防止重复。
sampler <- function(number_sample) {
  x <- sample(1:number_sample, replace = TRUE)
  pre_test <- lapply(x, function(single_number) diff(which(single_number == x)) == 1)
  test <- any(sapply(pre_test, any))

  if (test) sampler(number_sample) else x
}

sampler(100)
sampler(10)
custom.sampling <- function(pool, elems) {
  # arg check
  if ((!is.vector(pool) )|
    elems < 2)
    stop("Bad params")

  #init and proceed
  tmp <- c(1,1)
  while (sum(table(tmp) == 2) >0 ){
    tmp <- sample(pool, size = elems, replace = T)
  }
  return(tmp)
}

pool <- 0:9
elems <- 5
custom.sampling(pool, elems)
values <- 1:4 #values to sample from
len <- 20 #number of samples
samp <- sample(values,1) #initialise variable
length(samp) <- len
sapply(2:len, function(i) samp[i] <<- sample(setdiff(values, samp[i-1]), 1))

samp
[1] 2 1 4 1 4 3 2 4 3 1 3 1 4 3 4 1 3 1 4 2