[R] expand.grid() function

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Mon Jun 23 16:07:26 CEST 2008


Expand.grid works with lists too.

> expand.grid(rep(list(c("u", "l")), 3))
  Var1 Var2 Var3
1    u    u    u
2    l    u    u
3    u    l    u
4    l    l    u
5    u    u    l
6    l    u    l
7    u    l    l
8    l    l    l

This is probably as concise as is can get.

HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be 
www.inbo.be 

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Gavin Simpson
Verzonden: maandag 23 juni 2008 15:51
Aan: Megh Dal
CC: r-help op stat.math.ethz.ch
Onderwerp: Re: [R] expand.grid() function

On Mon, 2008-06-23 at 06:16 -0700, Megh Dal wrote:
> Hi,
>    
>   I have one question on expand.grid() function.
>    
>   When I write following syntax :expand.grid(c("u", "l"), c("u", "l"),
> c("u", "l")) I get following as desired :
>     Var1 Var2 Var3
> 1    u    u    u
> 2    l    u    u
> 3    u    l    u
> 4    l    l    u
> 5    u    u    l
> 6    l    u    l
> 7    u    l    l
> 8    l    l    l
> 
>   However I wanted to write that in more concise manner. Therefore I
> tried : expand.grid(rep(c("u", "l"), 3)). But I did not get answer
> that I previously got. Can people here clarify me why it is not like
> that? Then what would be the mose concise way to do that?

In the first case, you have three vectors of length 2 as arguments to
expand.grid, but in the second, you have a single vector of length 6. In
the latter case, expand.grid can't expand a single vector, hence the
single column result.

This is the closest I got to what you want:

as.matrix(expand.grid(split(rep(c("u","l"), times = 3), 
          factor(rep(1:3, each = 2)))))

Which gives:
> as.matrix(expand.grid(split(rep(c("u","l"), times = 3),
factor(rep(1:3, each = 2)))))
     1   2   3  
[1,] "u" "u" "u"
[2,] "l" "u" "u"
[3,] "u" "l" "u"
[4,] "l" "l" "u"
[5,] "u" "u" "l"
[6,] "l" "u" "l"
[7,] "u" "l" "l"
[8,] "l" "l" "l"

But that isn't particularly concise...

HTH

G
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
R-help op r-project.org 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.



More information about the R-help mailing list