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

  • Home
  • SEARCH
  • 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 7954007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:17:44+00:00 2026-06-04T03:17:44+00:00

In a question here on SO ( LINK ) a poster asked a question

  • 0

In a question here on SO (LINK) a poster asked a question and I gave an answer that works but there’s a part that bugs me, creating a list from a vector to pass as a list of indices. So les say I have this vector:

n <- 1:10
#> n
# [1]  1  2  3  4  5  6  7  8  9 10

Let’s say I want to break it up into a list of vectors and each vector is of length 3. What’s the best (shortest amount of code & or fastest) way to accomplish this? We want to toss out item 10 since it there’s a remainder of 1 (10 %% 3) from 10/3 (length(n) - 10 %% 3).

This is the desired outcome

list(1:3, 4:6, 7:9)

This will give us the indices of those that can’t make a group of three:

(length(n) + 1 - 10 %% 3):length(n)

EDIT

Here’s an interesting approach posted by Wojciech Sobala on the other thread this is linked to (I asked them to answer here and if they do I’ll remove this edit)

n <- 100
l <- 3
n2 <- n - (n %% l)
split(1:n2, rep(1:n2, each=l, length=n2))

As a function:

indices <- function(n, l){
    if(n > l) stop("n needs to be smaller than or equal to l")
    n2 <- n - (n %% l)
    cat("numbers", (n + 1 - n %% l):n, "did not make an index of length", l)
    split(1:n2, rep(1:n2, each=l, length=n2))
}
  • 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-04T03:17:46+00:00Added an answer on June 4, 2026 at 3:17 am

    Not sure if this does the job?

    x = function(x, n){ 
        if(n > x) stop("n needs to be smaller than or equal to x")
        output = matrix(1:(x-x%%n), ncol=(x-x%%n)/n, byrow=FALSE)
        output
    }
    

    Edit: changed the output to a list

    x = function(x, n){ 
        if(n > x) stop("n needs to be smaller than or equal to x")
        output = matrix(1:(x-x%%n), ncol=(x-x%%n)/n, byrow=TRUE)
        split(output, 1:nrow(output))
    }
    
    Example:
    x(10, 3)
    $`1`
    [1] 1 2 3
    
    $`2`
    [1] 4 5 6
    
    $`3`
    [1] 7 8 9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a precursor to this question here: Click link in DIV and show
Here is a link to a similar question with a good answer: Java Algorithm
I asked a question regarding Bi linear transformations and received this answer: From that
there is a similar question in here but for some reason I cannot make
This is a question that extends from the originally posted here: Link to loading-xaml
I have a question here, please take a look at the link autocorrelation function
Just a follow up question to this one here => link Is it possible
Here is a link to the code about this question: http://virtual.weltec.ac.nz/s13/blog/blog.html Basically, I am
Newbie question here but for some reason I cant figure this out. I have
Quick question here, i've got a process running that grabs RSS feeds and adds

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.