如何将特定长度和数量的随机连续间隙插入向量中?
How to insert random, continuous gaps of specific length and number into vector?
我需要创建几个具有不同随机数和连续间隙长度的数据集。我想用 NA 创建一个向量来与原始数据集相乘。这是我目前为一个数据集获得的代码(整个模拟将使用 MonteCarlo 进行):
For gap definition:
size1 <- sample(1:40, size=1)
length <- sample(1:480, size=size1)
Expressing as different vectors with NA:
vec <- list()
for (i in 1:length(length)){
vec[[i]] <- seq(1:length[i])
vec[[i]] <- replace(c(vec[[i]]), values=NA)
}
我还需要将这些向量随机插入,但连贯且不相邻(space 至少为一个)到特定长度的向量(例如 37000)。
x <- numeric(37000)
x <- replace(x, 1)
有谁知道这样做的好方法吗?
提前多多加油!
我不确定这是最好的方法,
但这是一个选项:
vector_length <- 37000L
num_gaps <- sample(40L, 1L) # 40 is the max amount of gaps
gap_lengths <- sample(480L, num_gaps) # 480 is the max length of a gap
# initialize
overlaps <- TRUE
gap_ends <- Inf
while (any(overlaps) || any(gap_ends > vector_length)) {
gap_starts <- sort(sample(vector_length, num_gaps))
gap_ends <- gap_starts + gap_lengths - 1L
# make sure the distance between each gap is at least 1
overlaps <- gap_starts[-1L] - gap_ends[-num_gaps] <= 1L
}
x <- numeric(vector_length)
for (i in seq_along(gap_starts)) {
x[gap_starts[i]:gap_ends[i]] <- NA_real_
}
我需要创建几个具有不同随机数和连续间隙长度的数据集。我想用 NA 创建一个向量来与原始数据集相乘。这是我目前为一个数据集获得的代码(整个模拟将使用 MonteCarlo 进行):
For gap definition:
size1 <- sample(1:40, size=1)
length <- sample(1:480, size=size1)
Expressing as different vectors with NA:
vec <- list()
for (i in 1:length(length)){
vec[[i]] <- seq(1:length[i])
vec[[i]] <- replace(c(vec[[i]]), values=NA)
}
我还需要将这些向量随机插入,但连贯且不相邻(space 至少为一个)到特定长度的向量(例如 37000)。
x <- numeric(37000)
x <- replace(x, 1)
有谁知道这样做的好方法吗? 提前多多加油!
我不确定这是最好的方法, 但这是一个选项:
vector_length <- 37000L
num_gaps <- sample(40L, 1L) # 40 is the max amount of gaps
gap_lengths <- sample(480L, num_gaps) # 480 is the max length of a gap
# initialize
overlaps <- TRUE
gap_ends <- Inf
while (any(overlaps) || any(gap_ends > vector_length)) {
gap_starts <- sort(sample(vector_length, num_gaps))
gap_ends <- gap_starts + gap_lengths - 1L
# make sure the distance between each gap is at least 1
overlaps <- gap_starts[-1L] - gap_ends[-num_gaps] <= 1L
}
x <- numeric(vector_length)
for (i in seq_along(gap_starts)) {
x[gap_starts[i]:gap_ends[i]] <- NA_real_
}