[R] Help with urnsamples and all possible combination

Boris Steipe boris.steipe at utoronto.ca
Sat May 23 20:36:06 CEST 2015


Actually your approach doesn't get around making a matrix of that size. If you look at the code of urnsamples, (in prob:::urnsamples.default), you'll find that it creates an intermediate object using expand.grid(). So you might as well create the matrix yourself, extract all rows for which max(table(<your row>)) < 4, and sample() from those (or use them all).

Or, if you are willing to accept the slight shift in probabilities of selected elements, and number of combinations you need is limited, and you can accept the small possibility that a combination will be repeated, you could just replicate your vector three times and sample without replacement:

s <- 7
x <- 1:25
limit <- 20
A <- matrix(0, nrow=limit, ncol=s)
for (i in 1:limit) {
    A[i, ] <- sort(sample(rep(x,3), size = s, replace = FALSE))
}
A


Cheers,
B.



On May 23, 2015, at 6:59 AM, MGalla <michelle.galla at gmx.de> wrote:

> Hello, 
> 
> i need some help to use urnsamples from the package "prob".
> 
> At the moment i use it in this way
> 
> A<-as.matrix(urnsamples(1:25, size = 7,ordered=FALSE,replace=TRUE))
> 
> So I get the numbers 1 to 25 and choose 7 of them, but it is possible to get
> one number more than one time. 
> But I want additionally that I get a number not more than three times. So
> for example "1111222" is no 
> combination, but "1112223" is allowed. 
> 
> I know that it should be possible to create A in my way and than delete all
> "wrong" combinations, 
> but i will work also with bigger size than 7, so the Matrix A will get to
> large. 
> 
> So my question is, if there is a option to give urnsamples the limit to
> choose every object not more than three times? Or is there another way to
> get the combinations I want. 
> 
> Thanks, 
> Michelle Galla
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Help-with-urnsamples-and-all-possible-combination-tp4707560.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list