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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:41:54+00:00 2026-06-06T01:41:54+00:00

I want to loop over a range of numbers from 1:n , when n

  • 0

I want to loop over a range of numbers from 1:n, when n is the length of vector v in R. Usually, I would use the for (i in 1:length(v)) syntax, but this fails when n == 0.

What is the idiomatic way to do this loop? At the moment I do the followin, but it seems a little ugly:

# This is in my standard library
rng <- function(n)seq(from=1, to=n, length.out=n)

# Now when I come to the for loop:
for(i in rng(length(v))){
   print(paste("I ate", i, "kg of brocolli today"))
}

And yes, I know its better to vectorise, but there are situations when vectorisation isn’t possible or would require so much extra work that it is much harder to read the code.

  • 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-06T01:41:56+00:00Added an answer on June 6, 2026 at 1:41 am

    It would be better to use seq_along:

    > v <- letters[1:3]
    > for (i in seq_along(v)) print(c(i, v[i]))
    [1] "1" "a"
    [1] "2" "b"
    [1] "3" "c"
    > 
    > v <- numeric(0)
    > for (i in seq_along(v)) print(c(i, v[i]))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a loop that will iterate over several strings, but unable
I have this list: names = ['john','Jonh','james','James','Jardel'] I want loop over the list and
I know that I can accomplish this via iterating over the first range, but
I have some divs that i use as containers, i want to loop over
I want to loop over a series of files in a directory in batches
I want to loop over the files that begin with '.' in directory x
I have an object of type Hash that I want to loop over via
I have the code below and I want to loop this every 5 seconds.
This for loop iterates over all elements in a list: for item in my_list:
i want to loop over a period of time in tsql, and print the

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.