Iterate over cartesian product of vectors

I have the following nested loop:

for (x in xs) {
    for (y in ys) {
        # Do something with x and y
    }
}

Which I’d like to flatten so I thought of building a Cartesian product of the two vectors xs and ys and iterating over the result. In Python, this would be trivial:

for xy in product(xs, ys):
    # x, y = xy[0], xy[1]

But in R, the simplest equivalent I’ve found looks daunting:

xys <- expand.grid(xs, ys)
for (i in 1 : nrow(xys)) {
    xy <- as.vector(xys[i, ])
    # x <- xy[1], y <- xy[2]
}

Surely there must be a better way, no? (To clarify, I don’t want to iterate over an index … I think there must be a way to directly iterate over the tuples in the product.)

Answers


You can use the apply function to apply a function to each row of your data frame. Just replace "your function" with your actual function.

# example data
xs <- rnorm(10)
ys <- rnorm(10)    

apply(expand.grid(xs, ys), 1, FUN = function(x) {"your function"})

This is a very basic example. Here, the sum of both values in a row is calculated:

apply(expand.grid(xs, ys), 1, FUN = function(x) {x[1] + x[2]})

Here is a variant that uses named arguments (xs, ys) instead of indices (x[1], x[2]):

myfun <- function(xs, ys) xs + ys
arguments <- expand.grid(xs = rnorm(10), ys = rnorm(10))
apply(arguments, 1, function(x)do.call(myfun, as.list(x)))

R has a different paradigm than Python, so don't expect it to have generators or tuples -- we have vectors and indices for that.

This way, to map a function on a Cartesian product simply call

outer(xs,ys,function(x,y) ...)

and undim the result if you wish.

EDIT: In case xs or ys are something more complex than base vectors, one option is to use indices, i.e.

outer(seq(a=xs),seq(a=ys),function(xi,yi) ... xs[[xi]]/ys[xi,]/etc. ...)

or map a function on a bit hand-crafted product using mapply

mapply(function(x,y) ...,xs,rep(ys,each=length(xs)))

Need Your Help

Display current logged in user's profile

php mysql

I've been working on code for student registration. I finished coding for my log in.

Installing TFS 2008 Workgroup Edition on Windows Server 2003 with SQL Server 2008

sql-server windows tfs visual-studio-team-system

I ran into an issue when installing Team Foundation Server 2008 Workgroup Edition. I have a Windows Server 2003 SP2 machine that's practically new. I have installed SQL Server 2008 with almost all ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.