rs

how to generate elements not included in my sample


This is a bit trivial but how can i generate the set of numbers in x not included in the sample.

x=rnorm(6,0,1)
k=sample(x,3)

Solution

  • Rather than sampling the values, sample the indexes.

    set.seed(15)
    (x <-rnorm(6,0,1))
    # [1]  0.2588229  1.8311207 -0.3396186  0.8971982  0.4880163 -1.2553858
    idx <- sample(length(x),3)
    (selected <- x[idx])
    # [1]  0.8971982 -1.2553858  0.4880163
    (notselected <- x[-idx])
    # [1]  0.2588229  1.8311207 -0.3396186