Hello! I have a list of variable length. One example is: X=vector("list",3) X[[1]]=1:2 X[[2]]=1:2 X[[3]]=1:2 How could I run expand.grid on the elements of X so that the results would be the same as expand.grid(1:2,1:2,1:2)? Thank you! Dimitri -- Dimitri Liakhovitski gfk.com <http://marketfusionanalytics.com/> [[alternative HTML version deleted]]
Oops, it was easier than I thought: expand.grid(X) Dimitri On Fri, Feb 1, 2013 at 8:48 AM, Dimitri Liakhovitski < dimitri.liakhovitski@gmail.com> wrote:> Hello! > I have a list of variable length. One example is: > X=vector("list",3) > X[[1]]=1:2 > X[[2]]=1:2 > X[[3]]=1:2 > How could I run expand.grid on the elements of X so that the results would > be the same as expand.grid(1:2,1:2,1:2)? > > Thank you! > Dimitri > > -- > Dimitri Liakhovitski > gfk.com <http://marketfusionanalytics.com/> > >-- Dimitri Liakhovitski gfk.com <http://marketfusionanalytics.com/> [[alternative HTML version deleted]]
Hi, expand.grid(X) # Var1 Var2 Var3 #1 1 1 1 #2 2 1 1 #3 1 2 1 #4 2 2 1 #5 1 1 2 #6 2 1 2 #7 1 2 2 #8 2 2 2 expand.grid(1:2,1:2,1:2) # Var1 Var2 Var3 #1 1 1 1 #2 2 1 1 #3 1 2 1 #4 2 2 1 #5 1 1 2 #6 2 1 2 # 1 2 2 #8 2 2 2 A.K. ----- Original Message ----- From: Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> To: r-help <r-help at r-project.org> Cc: Sent: Friday, February 1, 2013 8:48 AM Subject: [R] expand.grid on contents of a list Hello! I have a list of variable length. One example is: X=vector("list",3) X[[1]]=1:2 X[[2]]=1:2 X[[3]]=1:2 How could I run expand.grid on the elements of X so that the results would be the same as expand.grid(1:2,1:2,1:2)? Thank you! Dimitri -- Dimitri Liakhovitski gfk.com <http://marketfusionanalytics.com/> ??? [[alternative HTML version deleted]] ______________________________________________ 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.
Reasonably Related Threads
- Looping through rows of all elements of a list that has variable length
- grabbing from elements of a list without a loop
- Fastest way to compare a single value with all values in one column of a data frame
- Select only unique rows from a data frame
- Faster way of summing values up based on expand.grid