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

  • SEARCH
  • Home
  • 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 8297421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:24:44+00:00 2026-06-08T15:24:44+00:00

I can make this list by hand: list( list(n=1) , list(n=2), list(n=3) ) But

  • 0

I can make this list by hand:

list( list(n=1) , list(n=2), list(n=3) )

But how do I automate this, for instance if I want n to go up to 10? I tried as.list(1:10), which firstly is a different type of data structure, and secondly I couldn’t work out how to specify n.

I’m hoping the answer can be expanded to multiple element lists, e.g. all combinations of 1:3 and c(‘A’,’B’):

list( list(n=1,z='A') , list(n=2,z='A'), list(n=3,z='A'),
      list(n=1,z='B') , list(n=2,z='B'), list(n=3,z='B') )

Background: I’ll be using it along the lines of: lapply( outer_list, function(params) do.call(FUN,params) )

UPDATE:
It was difficult to choose which answer to give the tick to. I went with the expand.grid approach as it can scale to more than two parameters more easily; the use of mapply as shown in the comment makes the two examples above look reasonably compact and readable:

outer_list=with( expand.grid(n=1:10,stringsAsFactors=F),
    mapply(list, n=n, SIMPLIFY=F)
    )

outer_list=with( expand.grid(n=1:3,z=c('A','Z'), stringsAsFactors=F),
    mapply(list, n=n, z=z, SIMPLIFY=F)
    )

They violate the DRY principle, by repeating the parameter names in the mapply() call, which bothers me a little. So, when it bothers me enough I will use the alply call as shown in Sebastian’s answer.

  • 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-08T15:24:47+00:00Added an answer on June 8, 2026 at 3:24 pm
    vals <- expand.grid(n=1:3, z=c("A", "B"), 
                          KEEP.OUT.ATTRS=FALSE, stringsAsFactors=FALSE)
    
    library(plyr)
    alply(vals, 1, as.list)
    
    $`1`
    $`1`$n
    [1] 1
    
    $`1`$z
    [1] "A"
    
    
    $`2`
    $`2`$n
    [1] 2
    
    $`2`$z
    [1] "A"
    
    
    $`3`
    $`3`$n
    [1] 3
    
    $`3`$z
    [1] "A"
    
    
    $`4`
    $`4`$n
    [1] 1
    
    $`4`$z
    [1] "B"
    
    
    $`5`
    $`5`$n
    [1] 2
    
    $`5`$z
    [1] "B"
    
    
    $`6`
    $`6`$n
    [1] 3
    
    $`6`$z
    [1] "B"
    
    
    attr(,"split_type")
    [1] "array"
    attr(,"split_labels")
      n z
    1 1 A
    2 2 A
    3 3 A
    4 1 B
    5 2 B
    6 3 B
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I make this input transparent? <input type=text class=foo> I've tried this but
Ok to the Task List in VS sounds pretty great. But I can't make
How i can make this condition en VB.NET If e.KeyCode = [a-zA-Z0-9] Then 'Blah
How can I make this code work? #!/bin/bash ARRAYNAME='FRUITS' FRUITS=( APPLE BANANA ORANGE )
How can I make this second struct work, when the first struct has a
How can I make this print properly without using two printf calls? char* second
How can I make this sliding panel example to slide down on mouseover and
How can I make this code work? class Meta @array = [:a,:b] def self.method_missing(name,
How can I make this link use the child selector without changing or removing
how can I make this jquery script close all previously opened children when entering

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.