[R] Sample of a subsample

David Studer studerov at gmail.com
Mon Sep 25 19:27:47 CEST 2017

Hello everybody!

I have the following problem: I'd like to select a sample from a subsample
in a dataset. Actually, I don't want to select it, but to create a new
variable sampleNo that indicates to which sample (one or two) a case
belongs to.

Lets suppose I have a dataset containing 40 cases:

data <- data.frame(var1=seq(1:40), var2=seq(40,1))

The first sample (n=10) I drew like this:

data$sampleNo <- 0
idx <- sample(seq(1,nrow(data)), size=10, replace=F)
data[idx,]$sampleNo <- 1

Now, (and here my problems start) I'd like to draw a second sample (n=10).
But this sample should be drawn from the cases that don't belong to the
first sample only. *Additionally, "var1" should be an even number.*

So sampleNo should be 0 for cases that were not drawn at all, 1 for cases
that belong to the first sample and 2 for cases belonging to the second
sample (= sampleNo equals 0 and var1 is even).

I was trying to solve it like this:

idx2<-data$var1%%2 & data$sampleNo==0
sample(data[idx2,], size=10, replace=F)

But how can I set sampleNo to 2?

Thank you very much for your help!


	[[alternative HTML version deleted]]

More information about the R-help mailing list