From: ONKELINX, Thierry <Thierry.ONKELINX_at_inbo.be>

Date: Mon, 23 Jun 2008 16:07:26 +0200

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_at_inbo.be

www.inbo.be

Date: Mon, 23 Jun 2008 16:07:26 +0200

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.

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_at_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_at_r-project.org [mailto:r-help-bounces_at_r-project.org]
Namens Gavin Simpson

Verzonden: maandag 23 juni 2008 15:51

Aan: Megh Dal

CC: r-help_at_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_at_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. ______________________________________________ R-help_at_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.Received on Mon 23 Jun 2008 - 14:31:50 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Mon 23 Jun 2008 - 15:30:47 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*