[R] Create a data frame of all possible unique combinations of factors

Q quagaars at gmail.com
Wed Jul 6 02:13:10 CEST 2011


Ah!  I like the idea.  Thanks!


jholtman wrote:
> 
> Missed that you wanted to elim duplicated:
> 
>> z <- expand.grid(test,test)
>> # add 'unique' key
>> z$key <- apply(z, 1, function(x)paste(sort(x), collapse=''))
>> str(z)
> 'data.frame':   16 obs. of  3 variables:
>  $ Var1: Factor w/ 4 levels "A","B","C","D": 1 2 3 4 1 2 3 4 1 2 ...
>  $ Var2: Factor w/ 4 levels "A","B","C","D": 1 1 1 1 2 2 2 2 3 3 ...
>  $ key : chr  "AA" "AB" "AC" "AD" ...
>  - attr(*, "out.attrs")=List of 2
>   ..$ dim     : int  4 4
>   ..$ dimnames:List of 2
>   .. ..$ Var1: chr  "Var1=A" "Var1=B" "Var1=C" "Var1=D"
>   .. ..$ Var2: chr  "Var2=A" "Var2=B" "Var2=C" "Var2=D"
> 

--
View this message in context: http://r.789695.n4.nabble.com/Create-a-data-frame-of-all-possible-unique-combinations-of-factors-tp3647338p3647415.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list