[R] constrained sample

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu May 6 20:22:01 CEST 2004


Possible something like this ?

> grp <- factor(rep(c("odd", "even"), 3)); val <- c(1,2,3,4,5,6)
> data.frame(grp, val)
   grp val
1  odd   1
2 even   2
3  odd   3
4 even   4
5  odd   5
6 even   6
> ( newval.list <- tapply(val, grp, sample) )
$even
[1] 6 4 2

$odd
[1] 3 1 5

> for(i in 1:length(newval.list)){
       val[ grp == names(newval.list)[i] ] <- newval.list[[i]]
  }
> data.frame(grp, val)
   grp val
1  odd   3
2 even   6
3  odd   1
4 even   4
5  odd   5
6 even   2



On Thu, 2004-05-06 at 19:00, Lutz Prechelt wrote:
> I'm afraid I do not understand what you need.
> What is sampled from where and what do you need to keep constant?
> And what is the relationship between the 'column' and the 'factor column'?
> 
> Maybe you can provide a complete example and counterexample?
> 
>   Lutz
> 
> > Is there an way to do a randomization (with sample) on a 
> > column and that 
> > maintain fixed the occurrence totals (i.e, numbers different 
> > of zeros) on a factor column ?
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list