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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:25:48+00:00 2026-05-25T19:25:48+00:00

I have a routine that generates a series of data vectors, one iteration at

  • 0

I have a routine that generates a series of data vectors, one iteration at a time. I would like to find a way to “grow” either a list or a matrix that holds these vectors. I tried to create a list,

PROTECT( myList = allocVector( VECSXP, 1 ) )

But is there a way to grow the list, by pushing a vector element in the end?

Also, I wouldn’t mind using a matrix, since the vectors I generate are of the same length.

  • 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-25T19:25:49+00:00Added an answer on May 25, 2026 at 7:25 pm

    Rf_lengthgets in Rinternals.h; implemented in builtin.c:lengthgets. The returned pointer needs to be PROTECTed, so one pattern is

    SEXP myList;
    PROTECT_INDEX ipx;
    PROTECT_WITH_INDEX(myList = allocVector( VECSXP, 1 ), &ipx);
    REPROTECT(mylist = Rf_lengthgets(mylist, 100), ipx);
    

    If one were growing a list based on some unknown stopping condition, the approach might be like in R, with pre-allocate and fill followed by extension; the following is psuedo-code:

    const int BUF_SIZE = 100;
    PROTECT_INDEX ipx;
    SEXP myList;
    int i, someCondition = 1;
    
    PROTECT_WITH_INDEX(myList=allocVector(VECSXP, BUF_SIZE), &ipx);
    for (i = 0; some_condition; ++i) {
        if (Rf_length(myList) == i) {
            const int len = Rf_length(myList) + BUF_SIZE;
            REPROTECT(myList = Rf_lengthgets(mYlist, BUF_SIZE), &ipx);
        }
        PROTECT(result = some_calculation();
        SET_VECTOR_ELT(myList, i, result);           
        UNPROTECT(1);
        // set some_condition
    }
    Rf_lengthgets(myList, i); // no need to re-PROTECT; we're leaving C
    UNPROTECT(1)
    return myList;
    

    This performs a deep copy of myList, so can become expensive and in some ways if ht emain objective to evaluate some_calculation, then it seems like it’s easier and not too much less efficient to do the pre-allocate and extend operations in an R loop, calling some_calculation and doing assignment inside the loop.

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

Sidebar

Related Questions

I have a bunch of code in a routine that looks a bit like
I have a T-SQL routine that copies user information from one table 'Radius' to
I have 2 ( core , core-web ) projects such a way that one
I have this routine that works well, but it messes up as it counts
I have a routine that examines thousands of records looking for discrepancies. This can
I have a routine that dynamically changes a select list's selected option when the
I'm developing an MVC application and I have a routine that gets the currently
I have a jquery routine that calls an MVC action which will do a
Good Day, I have a simple working routine in Perl that swaps two words:
I have a web application I've developed that has a fairly complex save routine.

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.