[R] all possible combinations of list elements

From: Eberhard F Morgenroth <emorgenr_at_uiuc.edu>
Date: Wed 04 Jan 2006 - 11:57:17 EST


I have a list as follows

P <- list(A  = c("CS", "CX"),
          B  = 1:4,
          Y  = c(4, 9))

I now would like to prepare a new list where the rows of the new list provide all possible combinations of the elements in the orginal list. Thus, the result should be the following

CS	1	4
CS	1	9
CS	2	4
CS	2	9
CS	3	4
CS	3	9
CS	4	4
CS	4	9
CX	1	4
CX	1	9
CX	2	4
CX	2	9
CX	3	4
CX	3	9
CX	4	4
CX	4	9

Is there a simple routine in R to create this list of all possible combinations? The routine will be part of a function with the list "P" as an input. "P" will not always have the same number of elements and each element in the list "P" may have different numbers of values.

Thanks,
Eberhard Morgenroth



Eberhard Morgenroth, Assistant Professor of Environmental Engineering University of Illinois at Urbana-Champaign 3219 Newmark Civil Engineering Laboratory, MC-250 205 North Mathews Avenue, Urbana, IL 61801, USA Email: emorgenr@uiuc.edu
http://cee.uiuc.edu/research/morgenroth

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jan 04 12:05:39 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:46 EST