[R] Conditional Sampling

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue Jan 12 14:21:24 CET 2010


On 12-Jan-10 12:58:13, ehcpieterse wrote:
> Hi,
> 
> I am hoping someone can help me with a sampling question.
> 
> I am using the following function to sample 10 unique observations:
> x <- sample(1:100, 10, replace=F)
> Given the first 10 observations, I need to sample another 5 unique
> observations from the remainder. I essentially want to do a Monte
> Carlo type analysis on the results.
> 
> I would appreciate any feedback.
> 
> Thanks
> -- 

If your second sampling is to be on the same footing as the first,
i.e. a random subset of 5 out of the remaining 90, then this is
equivalent to sampling 15 in the first place and using the first
10 of these as your first sample:

  set.seed(54321)
  x0 <- sample(1:100, 15, replace=F)
  x  <- x0[1:10]
  y  <- x0[-(1:10)]
  x0
  # [1] 43 50 18 27 21 83  5 20 32 34 13 61  4 57 86
  x
  # [1] 43 50 18 27 21 83  5 20 32 34
  y
  # [1] 13 61  4 57 86

  set.seed(54321)
  sample(1:100, 10, replace=F)
  # [1] 43 50 18 27 21 83  5 20 32 34

However, if the manner of taking the second sample from the
remainder will depend on the results of the first sample, then
further consideration is necessary. If this is the case, can you
indicate how the values in the first sample would influence how
the second sample is to be obtained?

Another approach, which leaves more options, is to use sample.int():

  set.seed(54321)
  X <- (1:100) ## (or any other 100 values)
  n <- sample.int(100,10,replace=FALSE) ## returns subset of (1:100)
  x <- X[n]
  Y <- X[-n]
  y <- sample(Y,5,replace=FALSE)

  x
  # [1] 43 50 18 27 21 83  5 20 32 34 ## (as before)
  y
  # [1] 14 70  4 66 96                ## (as before)

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 12-Jan-10                                       Time: 13:21:21
------------------------------ XFMail ------------------------------



More information about the R-help mailing list