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:29:43 -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
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>