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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:07:37+00:00 2026-06-09T12:07:37+00:00

The following code is commonly seen on SO when it comes to iterating over

  • 0

The following code is commonly seen on SO when it comes to iterating over the index values of a collection:

for (i in 1:length(x)) {
  # ...
}

The code misbehaves when the collection is empty because 1:length(x) becomes 1:0 which gives i the values 1 and 0.

iterate <- function(x) {
    for (i in 1:length(x)) {
      cat('x[[', i, ']] is', x[[i]], '\n')
    }
}

> iterate(c(1,2,3))
x[[ 1 ]] is 1 
x[[ 2 ]] is 2 
x[[ 3 ]] is 3 

> iterate(c())
x[[ 1 ]] is  
x[[ 0 ]] is

I recall seeing an elegant idiom for defining a sequence that has no elements when x is empty but I cannot remember it. What idiom do you use?

  • 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-09T12:07:38+00:00Added an answer on June 9, 2026 at 12:07 pm

    Either seq or seq_along give you something more reasonable when your object of interest is empty.

    > x <- NULL
    > seq(x)
    integer(0)
    > seq_along(x)
    integer(0)
    > x <- rnorm(5)
    > seq(x)
    [1] 1 2 3 4 5
    > seq_along(x)
    [1] 1 2 3 4 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code: <?php $test_array = array(); $test_array['string_index'] = data in string index; $test_array[] =
Following code if (_LastMouseClickArea != null) { if (_LastMouseClickArea.GridItem == GridItem.DataCell) {... fails because
Following code produces a nested array as a result for keys containing three items:
Following code takes like 2500 milliseconds on an i7-*3.4 GHz windows-7 64-bit computer to
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
Following code gets executed whenever I want to persist any entity. Things seems to
Following code is a pretty simple and complete JQuery Dialog. Everything works. Problem is
Following code is generated by a for loop. <form action=saveresponse.php method=POST name=mainForm> <input class=cbox_yes
Following code iterates through many data-rows, calcs some score per row and then sorts

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.