expand.grip for permutations

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

expand.grip for permutations

trekvana
if i were to have a block size of 4 people and i want to assign a treatment combination to the entire block, there would be 16 different treatment combinations (TTTT, TTTP, TTPP, PTTP, etc.)

i am trying to get all 16 permutations and i am able to use this code below.

drugs=c('P','T');
comb=expand.grid(drugs,drugs,drugs,drugs)

for a block size of 3 the code would be comb=expand.grid(drugs,drugs,drugs) and for a block size of 2 it would be comb=expand.grid(drugs,drugs).

my question is whether there is a way to automatically create the comb variable. i tried using expand.grid(rep(drugs, block.size) but that didn't work.

any help on how i can proceed? thanks
Reply | Threaded
Open this post in threaded view
|

Re: expand.grip for permutations

arun kirshna
This post has NOT been accepted by the mailing list yet.
Hi,
Try this:
fun1<-function(x,n){
 res<-do.call(expand.grid,rep(list(x),n))
 res}
  fun1(drugs,3)[1:3,]
#  Var1 Var2 Var3
#1    P    P    P
#2    T    P    P
#3    P    T    P
 head(fun1(drugs,5))
#  Var1 Var2 Var3 Var4 Var5
#1    P    P    P    P    P
#2    T    P    P    P    P
#3    P    T    P    P    P
#4    T    T    P    P    P
#5    P    P    T    P    P
#6    T    P    T    P    P
A.K.
Reply | Threaded
Open this post in threaded view
|

Re: expand.grip for permutations

Michael Weylandt
In reply to this post by trekvana
On Wed, Oct 31, 2012 at 8:18 PM, trekvana <[hidden email]> wrote:
> if i were to have a block size of 4 people and i want to assign a treatment
> combination to the entire block, there would be 16 different treatment
> combinations (TTTT, TTTP, TTPP, PTTP, etc.)
>
> i am trying to get all 16 permutations and i am able to use this code below.
>
> drugs=c('P','T');
> comb=expand.grid(drugs,drugs,drugs,drugs)

do.call(expand.grid, rep(list(drugs),n))

where n is 3,4,5...

Cheers,
Michael

>
> --
> View this message in context: http://r.789695.n4.nabble.com/expand-grip-for-permutations-tp4648067.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: expand.grip for permutations

William Dunlap
In reply to this post by trekvana
Instead of  
   rep(drugs, block.size)
use
   rep(list(drugs), block.size)
as the argument to expand.grid.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of trekvana
> Sent: Wednesday, October 31, 2012 1:18 PM
> To: [hidden email]
> Subject: [R] expand.grip for permutations
>
> if i were to have a block size of 4 people and i want to assign a treatment
> combination to the entire block, there would be 16 different treatment
> combinations (TTTT, TTTP, TTPP, PTTP, etc.)
>
> i am trying to get all 16 permutations and i am able to use this code below.
>
> drugs=c('P','T');
> comb=expand.grid(drugs,drugs,drugs,drugs)
>
> for a block size of 3 the code would be comb=expand.grid(drugs,drugs,drugs)
> and for a block size of 2 it would be comb=expand.grid(drugs,drugs).
>
> my question is whether there is a way to automatically create the comb
> variable. i tried using expand.grid(rep(drugs, block.size) but that didn't
> work.
>
> any help on how i can proceed? thanks
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/expand-grip-for-
> permutations-tp4648067.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|

Re: expand.grip for permutations

trekvana
This post was updated on .
In reply to this post by Michael Weylandt
thank you all! that worked!