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 7724265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:38:43+00:00 2026-06-01T04:38:43+00:00

I would like to transform the following nested for loop first <- c(1, 2,

  • 0

I would like to transform the following nested for loop

first <- c(1, 2, 3)
second <- c(1, 2, 3)

dummy = matrix(double(), len(first), len(second))
c <- list()
c$sum <- dummy
c$times <- dummy

for (i in 1:len(first)) {
    for (j in 1:len(second)) {
        c$sum[i, j] <- first[i] + second[j]
        c$times[i, j] <- first[i] * second[j]
    }
}

c

into code using foreach and get the same list of matrices as a result. I tried many different things but the closest “result” is this:

x <- foreach(b = second, .combine = "cbind") %:% foreach(a = first, .combine = "c") %do% {
            c <- list()
            c$sum <- a+b
            c$times <- a*b
            out <- c
            }
x

How to get this list of matrices right using foreach?

EDIT: One possibility is using a result and transform it after calling foreach:

res <- list()
res$sum <- x[rownames(x)=="sum", ]
rownames(res$sum) <- NULL
colnames(res$sum) <- NULL
res$times <- x[rownames(x)=="times", ]
rownames(res$times) <- NULL
colnames(res$times) <- NULL
res

How to “parametrize” foreach so there is no need to transform results?

  • 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-01T04:38:45+00:00Added an answer on June 1, 2026 at 4:38 am

    You “just” have to provide the correct .combine function.
    If you only have numbers, you can return an array rather than a list.

    library(foreach)
    library(abind)
    first <- 1:3
    second <- 4:5
    x <- 
      foreach(b = second, .combine = function(...) abind(..., along=3)) %:% 
      foreach(a = first,  .combine = rbind) %do% {
        c( sum=a+b, times=a*b )
      }
    

    If you really need lists, writing the combining functions is much harder.
    Instead, you can build a data.frame, and reshape it afterwards, if needed.

    x <- 
      foreach(b = second, .combine = rbind) %:% 
      foreach(a = first,  .combine = rbind) %do% {
        data.frame(a=a, b=b, sum=a+b, times=a*b )
      }
    library(reshape2)
    list(
      sum   = dcast(x, a ~ b, value.var="sum"  )[,-1],
      times = dcast(x, a ~ b, value.var="times")[,-1]
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that uses a for loop and I would like
I would like to transform the following lines of data into a structured table
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Hi does anyone know what the xsl would look like to transform this XML.
I have the following model class MyClass { id someRandomString } I would like
I am using xsl to transform xml to kml format. I would like to
I would like to change database column names of a BDC list webpart using
I am using Ruby on Rails v3.0.9 and I would like to transform an
I would like to use xslt to transform an xml file into an almost
Suppose I would like to match for the following pipe-delimited strings: std::string re(a|*|c); std::string

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.