Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8499635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:43:15+00:00 2026-06-11T00:43:15+00:00

I have the following nested loop: for (x in xs) { for (y in

  • 0

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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:43:16+00:00Added an answer on June 11, 2026 at 12:43 am

    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)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data and nested for loop: x <- c(12, 27, 21,
I have the following nested for loops. I want to parallelize the first loop
I have the following form for photo_album which uses the nested forms feature of
Assume the following: we have class B, which is a private class nested inside
I have following nested objects. I am using @Valid for validation in my controller.
I have the following nested dictionaries: Dictionary<int, Dictionary<string, object>> x; Dictionary<int, SortedDictionary<long, Dictionary<string, object>>>
Okay, I have the following create action #posts_controller, nested resource under discussions def create
How can I count nested list elements in Prolog? I have the following predicates
I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',
I have a problem with nested templates and their template specialization. Given the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.