s-news
[Top] [All Lists]

Re: FW: List of vectors from expand.grid

To: "Myers, Brent" <MyersDB@missouri.edu>
Subject: Re: FW: List of vectors from expand.grid
From: Spencer Graves <spencer.graves@PDF.COM>
Date: Fri, 30 May 2003 08:47:24 -0700
Cc: s-news@lists.biostat.wustl.edu
References: <7CC4280A9B5470499092940B3DDB5AD2082711@COL-EMAIL3.col.missouri.edu>
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02
OK.  How about:
> dfeg <- as.list(data.frame(t(expand.grid(1:2, 3:5))))
> dfeg[["ij"]] <- c("i", "j")
> dfeg
$X1:
[1] 1 3

$X2:
[1] 2 3

$X3:
[1] 1 4

$X4:
[1] 2 4

$X5:
[1] 1 5

$X6:
[1] 2 5

$ij:
[1] "i" "j"

hth.  spencer graves

Myers, Brent wrote:
> Thanks for your reply,
>
> Sorry, your suggestion is not quite what's needed.
>
> Expand.grid produces a dataframe having columns i and j and with i*j records.
>
> I want a list (length i*j) with i,,j pair vectors as list members. In other words, I want to collapse each observation in the data frame into a vector, adding the vector to a list.
>
> e.g.
> [[1]]:
>  [1] 1 1
>
> [[2]]:
> [1] 1 2
>
> [[3]]:
> [1] i j
>
>
> Thanks very much for trying,
> Brent
>
################################
How about:

 > as.list(data.frame(expand.grid(1:2, 3:5)))
$Var1:
[1] 1 2 1 2 1 2

$Var2:
[1] 3 3 4 4 5 5

hope this helps.  spencer graves

Myers, Brent wrote:
>>S community,
>>
>>I am using expand.grid to flexibly create a set of cell indexes(i,,j) from the dimensions of any matrix passed to a function.
>>
>>How can I get the expand.grid output into the form of a list of vectors?
>>
>>e.g list of cell index vectors
>>[[1]]:
>>[1] 1 1
>>
>>[[2]]:
>>[1] 1 2
>>
>>[[3]]:
>>[1] i j
>>
>>Here is my case
>>
>>#Get dimensions of any matrix or array
>>idx.limits <-dim(acube)
>>
>>#Generate the list of (planar) central cell index combinations
>>ccidx <- expand.grid (1:idx.limits[1], 1:idx.limits[2])
>>
>>I can try...
>>ccidx.matrix <- matrix(c(ccidx[[1]], ccidx[[2]]), length(ccidx[[1]]))
>>
>>to get a matrix, and then ...
>>ccidx.list <- apply(ccidx.matrix, 1, c)
>>
>>to collapse along the rows, But c doesn't work as expected. Sum adds along rows, but concatenate doesn't work that way?
>>
>>D. Brent Myers
>>Research Specialist - Agronomy
>>University of Missouri
>>158 Ag Engineering Building
>>Columbia, MO 65211
>>
>>
>>
>
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news




<Prev in Thread] Current Thread [Next in Thread>