jeudi 7 juin 2018

How to insert random, continuous gaps of specific length and number into vector?

I need to create several datasets with different, random number and length of continuous gaps. I thought of creating a vector with NA for multiplying with the original dataset. Here's the code I got so far for one dataset (the whole simulation will take place with 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)
    }

I further need to insert these vectors randomly, but coherently and not adjacent (with a space of at least one) into the vector of a specific length (e.g. 37000).

   x <- numeric(37000)
   x <- replace(x, 1)

Does anyone know a good way to do this? Cheers a lot in advance!




Aucun commentaire:

Enregistrer un commentaire