[R] all combinations with replacement not ordered
(Ted Harding)
Ted.Harding at wlandres.net
Thu Nov 7 18:04:54 CET 2013
On 07-Nov-2013 13:38:29 Konstantin Tretiakov wrote:
> Hello!
>
> I need to obtain all possible combinations with replacement when
> order is not important.
> E.g. I have a population x{1,2,3}.
> So I can get (choose(3+3-1,3)=) 10 combinations from this population
> with 'size=3'.
> How can I get a list of all that combinations?
>
> I have tried 'expand.grid()' and managed to get only samples where
> order is important.
> 'combn()' gave me samples without replacement.
>
> Best regards,
> Konstantin Tretyakov.
>From your description I infer that, from {1,2,3}, you want the result:
1 1 1
1 1 2
1 1 3
1 2 2
1 2 3
1 3 3
2 2 2
2 2 3
2 3 3
3 3 3
The following will do that:
u <- c(1,2,3)
unique(t(unique(apply(expand.grid(u,u,u),1,sort),margin=1)))
# [,1] [,2] [,3]
# [1,] 1 1 1
# [2,] 1 1 2
# [3,] 1 1 3
# [4,] 1 2 2
# [5,] 1 2 3
# [6,] 1 3 3
# [7,] 2 2 2
# [9,] 2 3 3
#[10,] 3 3 3
There may be a simpler way!
Ted.
-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 07-Nov-2013 Time: 17:04:50
This message was sent by XFMail
More information about the R-help
mailing list