Re: [R] Sample of a subsample

2017-09-25 Thread Bert Gunter
Yes. Beating a pretty weary horse, a slightly cleaner version of my prior offering using with(), instead of within() is: with(dat, dat[sampleNo[sample(var1[!var1%%2 & !sampleNo], 10, rep=FALSE)], "sampleNo"] <- 2) with() and within() are convenient ways to avoid having to repeatedly name the col

Re: [R] Sample of a subsample

2017-09-25 Thread Eric Berger
Hi David, I was about to post a reply when Bert responded. His answer is good and his comment to use the name 'dat' rather than 'data' is instructive. I am providing my suggestion as well because I think it may address what was causing you some confusion (mainly to use "which", but also the missing

Re: [R] Sample of a subsample

2017-09-25 Thread Bert Gunter
For personal aesthetic reasons, I changed the name "data" to "dat". Your code, with a slight modification: set.seed (1357) ## for reproducibility dat <- data.frame(var1=seq(1:40), var2=seq(40,1)) dat$sampleNo <- 0 idx <- sample(seq(1,nrow(dat)), size=10, replace=F) dat[idx,"sampleNo"] <-1 ## yi

[R] Sample of a subsample

2017-09-25 Thread David Studer
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: d