Re: [R] making array of lists

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon, 03 Mar 2008 20:41:29 -0500

Try this:

> a <- 1:2
> b <- 1:3
> g <- array(do.call(c, apply(expand.grid(a = a, b = b), 1, list)),
+ dim = c(length(a), length(b)))
> g

     [,1] [,2] [,3]
[1,] Integer,2 Integer,2 Integer,2
[2,] Integer,2 Integer,2 Integer,2
> g[[2,3]]

a b
2 3

On Mon, Mar 3, 2008 at 8:21 PM, Vadim Organovich <vogranovich_at_jumptrading.com> wrote:
> Dear R Users,
>
>
>
> Given two vectors, say a = seq(2) and b = seq(3), I want to make an 2*3 array, where (i,j) element is list(a=a[i], b=b[j]). I tried the outer() function but it generates an error message that I don't understand, see below.
>
>
>
> What do I do wrong?
>
>
>
> The expan.grid function is not good enough since I need a solution that works when a and b are not atomic, say a=list(list(x=1, y=2), list(x=2, y=1)).
>
>
>
> Thanks,
>
> Vadim
>
>
>
> > a <- seq(2); b <- seq(3)
> > outer(a, b, function(a, b) list(a=a, b=b))
> Error in dim(robj) <- c(dX, dY) :
> dims [product 6] do not match the length of object [2]
>
>
>
>
>
> [[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.
>



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 Tue 04 Mar 2008 - 01:43:54 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 Tue 04 Mar 2008 - 02:30:19 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.

list of date sections of archive