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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:40:38+00:00 2026-06-13T01:40:38+00:00

Consider the code below: foo = list(First List, 1, 2, 3) bar = function(x)

  • 0

Consider the code below:

foo = list("First List", 1, 2, 3)
bar = function(x) {
    cat("The list name is:", x[[1]], "\nThe items are:\n")
    for (i in 2:length(x))
        cat(x[[i]], "\n")
}
bar(foo)

The result will be:

The list name is: First List 
The items are:
1 
2 
3 

Now consider passing a list with no items, but a name:

baz = list("Second List")
bar(baz)

The result would be:

The list name is: Second List 
The items are:
Error in x[[i]] : subscript out of bounds

The error is because 2:length(x) will produce a sequence of c(2, 1) for the latter case bar(baz), so it tries to access baz[2] and it does not exist.

How to simply prevent this unwanted reverse iteration in a for loop in R?

  • 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-13T01:40:39+00:00Added an answer on June 13, 2026 at 1:40 am

    No need to loop over the list indices, you can just loop over a sub-list:

    > bar = function(x) {
    +     cat("The list name is:", x[[1]], "\nThe items are:\n")
    +     for (i in x[-1])
    +         cat(i, "\n")
    + }
    

    If there is a single item in your list, the sub-list will be empty and the for loop will be skipped.

    Edit: As GavinSimpson points out, this works well because your particular case did not really need to loop over indices. If indices were absolutely needed, then you would have to loop over seq_along(x[-1]) instead of x[-1] as Andrie showed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the Lua code below: local util = {} function util:foo(p) print (p or
Consider the code below: <?php class Base { protected $name = Base; public function
Consider the code below $('.item_select').each(List); function List() { var x= this.siblings('.fooClass'); } When I
I have a code snippet as below <CheckBox Name=cb Margin=1,2,1,0 IsChecked={Binding Path=IsManager} IsEnabled=True/> Consider
Consider the following example code: class Foo { }; class Bar : public Foo
In the code below, I've defined two overloaded methods named Bar . In Foo()
Consider the code below class Pin { string Name; //can be either Numerical or
A sample problem asks us to consider the code below and predict what will
Consider the code below: DummyBean dum = new DummyBean(); dum.setDummy(foo); System.out.println(dum.getDummy()); // prints 'foo'
Consider the code below: this.usedIds = 0; this.SendData = function(data) { var id =

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.