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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:15:12+00:00 2026-05-26T00:15:12+00:00

I know that I should avoid for-loops, but I’m not exactly sure how to

  • 0

I know that I should avoid for-loops, but I’m not exactly sure how to do what I want to do with an apply function.

Here is a slightly simplified model of what I’m trying to do. So, essentially I have a big matrix of predictors and I want to run a regression using a window of 5 predictors on each side of the indexed predictor (i in the case of a for loop). With a for loop, I can just say something like:

results<-NULL
window<-5
for(i in 1:ncol(g))
{
    first<-i-window #Set window boundaries
    if(first<1){
        1->first
    }
    last<-i+window-1
    if(last>ncol(g)){
        ncol(g)->last
    }
    predictors<-g[,first:last]

    #Do regression stuff and return some result
    results[i]<-regression stuff
}

Is there a good way to do this with an apply function? My problem is that the vector that apply would be shoving into the function really doesn’t matter. All that matters is the index.

  • 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-05-26T00:15:13+00:00Added an answer on May 26, 2026 at 12:15 am

    Using an apply function to do your regression is mostly a matter of preference in this case; it can handle some of the bookkeeping for you (and so possibly prevent errors) but won’t speed up the code.

    I would suggest using vectorized functions though to compute your first‘s and last‘s, though, perhaps something like:

    window <- 5
    ng <- 15 #or ncol(g)
    xy <- data.frame(first = pmax( (1:ng) - window, 1 ), 
                      last = pmin( (1:ng) + window, ng) )
    

    Or be even smarter with

    xy <- data.frame(first= c(rep(1, window), 1:(ng-window) ), 
                     last = c((window+1):ng, rep(ng, window)) )
    

    Then you could use this in a for loop like this:

    results <- list()
    for(i in 1:nrow(xy)) {
      results[[i]] <- xy$first[i] : xy$last[i]
    }
    results
    

    or with lapply like this:

    results <- lapply(1:nrow(xy), function(i) {
      xy$first[i] : xy$last[i]
    })
    

    where in both cases I just return the sequence between first and list; you would substitute with your actual regression code.

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

Sidebar

Related Questions

this is my first question here :) I know that I should not check
I know that I should put all the html elements in body tag, but
I know that we should only create repositories for Aggregate Roots and not for
I know that c++ code should be compiled and linked by g++, not gcc.
I know that a command line application should return 0 on success. But are
I know that some say that class variables (e.g. @@class_var ) should be avoid
I read in the docs that one should avoid using finish() - but I
I know that I should have schema of a table before calling NewRow method
I know that most links should be left up to the end-user to decide
I know that the following code should show and hide a tiny circular progress

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.