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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:14:58+00:00 2026-06-14T02:14:58+00:00

I am running a loop in R to find indices of a vector when

  • 0

I am running a loop in R to find indices of a vector when its elements are equal to elements of a reference vector.

As far as I know R, I need to declare the variable before the for-loop, but in this case I do not know the final length of my indices vector (see code below).
How can I create a variables that allows R to change its size during the for loop?

extract of my code:

k <- 1
for(i in 1:length(Lid.time)){
  ind <- which(Net.time==Lid.time[i])
  if(length(ind)>0){
    ind.Net[k] <- ind
    k <- k+1
  }
}

Notes about the code:
Lid.time is a vector of a different lenght than Net.time.
I need to find an array of indices that tells me where Net.time is equal to Lid.time. I do not know in advance how long will the ind.Net vector will be, so how can I declare the vector ind.Net?

Thanks for your help

  • 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-14T02:15:00+00:00Added an answer on June 14, 2026 at 2:15 am

    As Dason stated, match will work just fine for that specific task:

    >a <- seq(2,20,2)
       #[1]  2  4  6  8 10 12 14 16 18 20
    >b <- c(4,14,18)
    >match(b,a)
       #[1] 2 7 9  # The indices!
    >a %in% b #shorthand logical version of match
       #[1] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE
    

    But to answer your question of a vector of unknown length within a loop:

    Vector <- c()
    for(i in sample(1:100,20)) {
        if(i<50) {Vector <- append(Vector, i)}
        }
    length(HowLongIsThisVector)
    

    It will be different every time you run it because of sample.

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

Sidebar

Related Questions

I am running a loop, and I am trying to create a variable each
I am having some problems running a find loop inside of a subroutine when
Possible Duplicate: How to find an element within an element Im running a loop
I have a control loop running at high frequency and need to compute a
I am running a simple loop: var c=0; while(c<count){ theimg = $container.find('.img'+c+''); $thisX =
I am trying to set up a running loop, and to do so I've
I'm running a loop basically that will make an array that contains a million
I have a Parallel.ForEach loop running an intensive operation inside the body. The operation
I'm working with a long running parfor loop in matlab. parfor iter=1:1000 chunk_of_work(iter); end
I have a program in Octave that has a loop - running a function

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.