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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:01:01+00:00 2026-06-11T15:01:01+00:00

When I loop through a vector of vectors, the result of each loop is

  • 0

When I loop through a vector of vectors, the result of each loop is several vectors. I would expect the result of each loop to be a vector. Please see the following example:

foo <- seq(from=1, to=5, by=1)
bar <- seq(from=6, to=10, by=1)
baz <- seq(from=11, to=15, by=1)
vects <- c(foo,bar,baz)
for(v in vects) {print(v)}

# [1] 1
# [1] 2
# [1] 3
# [1] 4
# [1] 5
# [1] 6
# [1] 7
# [1] 8
# [1] 9
# [1] 10
# [1] 11
# [1] 12
# [1] 13
# [1] 14
# [1] 15

This is odd as I would expect three vectors given it (should) iterate three times given the vector, c(foo,bar,baz). Something like:

# [1]  1  2  3  4  5
# [1]  6  7  8  9 10
# [1] 11 12 13 14 15

Can anyone explain why I am getting this result (15 vectors) and how to achieve the result I am looking for (3 vectors)?

  • 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-11T15:01:02+00:00Added an answer on June 11, 2026 at 3:01 pm

    Look at what vects is:

    > vects
     [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15
    

    The c() joins (in this case) the three vectors, concatenating them into a single vector. In the for() loop, v takes on each values in vects in turn and prints it, hence the result you see.

    Did you want a list of the three separate vectors? If so

    > vects2 <- list(foo, bar, baz)
    > for(v in vects2) {print(v)}
    [1] 1 2 3 4 5
    [1]  6  7  8  9 10
    [1] 11 12 13 14 15
    

    In other words, form a list of the vectors, not a combination of the vectors.

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

Sidebar

Related Questions

I am trying to loop through a vectors elements and trying to find out
Suppose I have a vector<int> myvec and I want to loop through all of
I know I can loop through each character of two NSString objects using characterAtIndex:
Consider the following example: #include <iostream> #include <sstream> #include <vector> #include <wchar.h> #include <stdlib.h>
During program optimization, trying to optimize a loop that iterates through a vector, I
I want to loop through a vector to find records with a given ID.
I have my loop going through vector's elements. While in this loop some of
For loop iterates through the vector and show all strings on the console successfully:
For loop should iterate through std::vector and populate content. First for loop gives me
I want to loop through a vector and erase certain elements that correspond to

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.