Split vector at multiplicity of a number










4















I want to split a vector at a multiplicity of a number so giving vector c(1:100) I would like to receive vectors like:



c(1,11,21,31,41,51,61,71,81,91) 
c(2,12,22,32,42,52,62,72,82,92)
.....
c(10,20,30,40,50,60,70,80,90,100)


Note that i want the vector c(1,11,21,31,41,51,61,71,81,91) to be the FIRST in the list of new vectors, because I know that split(1:100,1:100 %% 10)[1] is c(10,20,30,40,50,60,70,80,90,100) which is not what I want



EDIT
The 1:100 example is just an example. In my app I want to split a vector of 256 numbers (random numbers not from 1 to 256) and divide it to 16 new vectors...










share|improve this question



















  • 4





    split(1:100,1:100 %% 10), perhaps?

    – joran
    Nov 14 '18 at 21:01











  • @joran, post as answer or find a duplicate? (I know, posting an answer is easier ...)

    – Ben Bolker
    Nov 14 '18 at 21:03











  • I tried it but then the first new vector is the one with 10,20,30 etc. So split(1:100,1:100 %% 10)[1] = c(10,20............). I want the first out of 10 to be the one with 1,11,21..... and the last with 10,20.30....

    – heisenberg7584
    Nov 14 '18 at 21:04











  • There may be a way to do better, but split(1:100,((1:100 %% 10) -1) %% 10) would give the order you want

    – Chris
    Nov 14 '18 at 21:09







  • 1





    better: split(1:100,0:99 %% 10)

    – Chris
    Nov 14 '18 at 21:12















4















I want to split a vector at a multiplicity of a number so giving vector c(1:100) I would like to receive vectors like:



c(1,11,21,31,41,51,61,71,81,91) 
c(2,12,22,32,42,52,62,72,82,92)
.....
c(10,20,30,40,50,60,70,80,90,100)


Note that i want the vector c(1,11,21,31,41,51,61,71,81,91) to be the FIRST in the list of new vectors, because I know that split(1:100,1:100 %% 10)[1] is c(10,20,30,40,50,60,70,80,90,100) which is not what I want



EDIT
The 1:100 example is just an example. In my app I want to split a vector of 256 numbers (random numbers not from 1 to 256) and divide it to 16 new vectors...










share|improve this question



















  • 4





    split(1:100,1:100 %% 10), perhaps?

    – joran
    Nov 14 '18 at 21:01











  • @joran, post as answer or find a duplicate? (I know, posting an answer is easier ...)

    – Ben Bolker
    Nov 14 '18 at 21:03











  • I tried it but then the first new vector is the one with 10,20,30 etc. So split(1:100,1:100 %% 10)[1] = c(10,20............). I want the first out of 10 to be the one with 1,11,21..... and the last with 10,20.30....

    – heisenberg7584
    Nov 14 '18 at 21:04











  • There may be a way to do better, but split(1:100,((1:100 %% 10) -1) %% 10) would give the order you want

    – Chris
    Nov 14 '18 at 21:09







  • 1





    better: split(1:100,0:99 %% 10)

    – Chris
    Nov 14 '18 at 21:12













4












4








4








I want to split a vector at a multiplicity of a number so giving vector c(1:100) I would like to receive vectors like:



c(1,11,21,31,41,51,61,71,81,91) 
c(2,12,22,32,42,52,62,72,82,92)
.....
c(10,20,30,40,50,60,70,80,90,100)


Note that i want the vector c(1,11,21,31,41,51,61,71,81,91) to be the FIRST in the list of new vectors, because I know that split(1:100,1:100 %% 10)[1] is c(10,20,30,40,50,60,70,80,90,100) which is not what I want



EDIT
The 1:100 example is just an example. In my app I want to split a vector of 256 numbers (random numbers not from 1 to 256) and divide it to 16 new vectors...










share|improve this question
















I want to split a vector at a multiplicity of a number so giving vector c(1:100) I would like to receive vectors like:



c(1,11,21,31,41,51,61,71,81,91) 
c(2,12,22,32,42,52,62,72,82,92)
.....
c(10,20,30,40,50,60,70,80,90,100)


Note that i want the vector c(1,11,21,31,41,51,61,71,81,91) to be the FIRST in the list of new vectors, because I know that split(1:100,1:100 %% 10)[1] is c(10,20,30,40,50,60,70,80,90,100) which is not what I want



EDIT
The 1:100 example is just an example. In my app I want to split a vector of 256 numbers (random numbers not from 1 to 256) and divide it to 16 new vectors...







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 21:15







heisenberg7584

















asked Nov 14 '18 at 20:53









heisenberg7584heisenberg7584

505




505







  • 4





    split(1:100,1:100 %% 10), perhaps?

    – joran
    Nov 14 '18 at 21:01











  • @joran, post as answer or find a duplicate? (I know, posting an answer is easier ...)

    – Ben Bolker
    Nov 14 '18 at 21:03











  • I tried it but then the first new vector is the one with 10,20,30 etc. So split(1:100,1:100 %% 10)[1] = c(10,20............). I want the first out of 10 to be the one with 1,11,21..... and the last with 10,20.30....

    – heisenberg7584
    Nov 14 '18 at 21:04











  • There may be a way to do better, but split(1:100,((1:100 %% 10) -1) %% 10) would give the order you want

    – Chris
    Nov 14 '18 at 21:09







  • 1





    better: split(1:100,0:99 %% 10)

    – Chris
    Nov 14 '18 at 21:12












  • 4





    split(1:100,1:100 %% 10), perhaps?

    – joran
    Nov 14 '18 at 21:01











  • @joran, post as answer or find a duplicate? (I know, posting an answer is easier ...)

    – Ben Bolker
    Nov 14 '18 at 21:03











  • I tried it but then the first new vector is the one with 10,20,30 etc. So split(1:100,1:100 %% 10)[1] = c(10,20............). I want the first out of 10 to be the one with 1,11,21..... and the last with 10,20.30....

    – heisenberg7584
    Nov 14 '18 at 21:04











  • There may be a way to do better, but split(1:100,((1:100 %% 10) -1) %% 10) would give the order you want

    – Chris
    Nov 14 '18 at 21:09







  • 1





    better: split(1:100,0:99 %% 10)

    – Chris
    Nov 14 '18 at 21:12







4




4





split(1:100,1:100 %% 10), perhaps?

– joran
Nov 14 '18 at 21:01





split(1:100,1:100 %% 10), perhaps?

– joran
Nov 14 '18 at 21:01













@joran, post as answer or find a duplicate? (I know, posting an answer is easier ...)

– Ben Bolker
Nov 14 '18 at 21:03





@joran, post as answer or find a duplicate? (I know, posting an answer is easier ...)

– Ben Bolker
Nov 14 '18 at 21:03













I tried it but then the first new vector is the one with 10,20,30 etc. So split(1:100,1:100 %% 10)[1] = c(10,20............). I want the first out of 10 to be the one with 1,11,21..... and the last with 10,20.30....

– heisenberg7584
Nov 14 '18 at 21:04





I tried it but then the first new vector is the one with 10,20,30 etc. So split(1:100,1:100 %% 10)[1] = c(10,20............). I want the first out of 10 to be the one with 1,11,21..... and the last with 10,20.30....

– heisenberg7584
Nov 14 '18 at 21:04













There may be a way to do better, but split(1:100,((1:100 %% 10) -1) %% 10) would give the order you want

– Chris
Nov 14 '18 at 21:09






There may be a way to do better, but split(1:100,((1:100 %% 10) -1) %% 10) would give the order you want

– Chris
Nov 14 '18 at 21:09





1




1





better: split(1:100,0:99 %% 10)

– Chris
Nov 14 '18 at 21:12





better: split(1:100,0:99 %% 10)

– Chris
Nov 14 '18 at 21:12












3 Answers
3






active

oldest

votes


















1














As in the comment, perfect choice would be to make it like this:



split(1:100,0:99 %% 10) 


In such case we can make it really generic so as:



split(vec, 0:(length(vec)-1) %% X)


where vec is the vector we want to divide and X is your multiplicity






share|improve this answer






























    2














    Sounds like you want split(1:100,1:100 %% 10). Some options for ordering include:



    x <- split(1:100,1:100 %% 10)
    c(tail(x,-1),head(x,1))


    or from the comments above,



    split(1:100,((1:100 %% 10) -1) %% 10)





    share|improve this answer






























      0














      This is uglier than joran's answer, but you could also do that in a loop, and use grep() to extract those numbers ending in a particular value:



      yourlist <- vector("list", 10)

      for (i in 1:10)
      yourlist[[i]] <- grep(paste0(i %% 10, "$"), 1:100)






      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308555%2fsplit-vector-at-multiplicity-of-a-number%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        As in the comment, perfect choice would be to make it like this:



        split(1:100,0:99 %% 10) 


        In such case we can make it really generic so as:



        split(vec, 0:(length(vec)-1) %% X)


        where vec is the vector we want to divide and X is your multiplicity






        share|improve this answer



























          1














          As in the comment, perfect choice would be to make it like this:



          split(1:100,0:99 %% 10) 


          In such case we can make it really generic so as:



          split(vec, 0:(length(vec)-1) %% X)


          where vec is the vector we want to divide and X is your multiplicity






          share|improve this answer

























            1












            1








            1







            As in the comment, perfect choice would be to make it like this:



            split(1:100,0:99 %% 10) 


            In such case we can make it really generic so as:



            split(vec, 0:(length(vec)-1) %% X)


            where vec is the vector we want to divide and X is your multiplicity






            share|improve this answer













            As in the comment, perfect choice would be to make it like this:



            split(1:100,0:99 %% 10) 


            In such case we can make it really generic so as:



            split(vec, 0:(length(vec)-1) %% X)


            where vec is the vector we want to divide and X is your multiplicity







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 21:43









            jake-fergusonjake-ferguson

            10211




            10211























                2














                Sounds like you want split(1:100,1:100 %% 10). Some options for ordering include:



                x <- split(1:100,1:100 %% 10)
                c(tail(x,-1),head(x,1))


                or from the comments above,



                split(1:100,((1:100 %% 10) -1) %% 10)





                share|improve this answer



























                  2














                  Sounds like you want split(1:100,1:100 %% 10). Some options for ordering include:



                  x <- split(1:100,1:100 %% 10)
                  c(tail(x,-1),head(x,1))


                  or from the comments above,



                  split(1:100,((1:100 %% 10) -1) %% 10)





                  share|improve this answer

























                    2












                    2








                    2







                    Sounds like you want split(1:100,1:100 %% 10). Some options for ordering include:



                    x <- split(1:100,1:100 %% 10)
                    c(tail(x,-1),head(x,1))


                    or from the comments above,



                    split(1:100,((1:100 %% 10) -1) %% 10)





                    share|improve this answer













                    Sounds like you want split(1:100,1:100 %% 10). Some options for ordering include:



                    x <- split(1:100,1:100 %% 10)
                    c(tail(x,-1),head(x,1))


                    or from the comments above,



                    split(1:100,((1:100 %% 10) -1) %% 10)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 21:12









                    joranjoran

                    136k19328388




                    136k19328388





















                        0














                        This is uglier than joran's answer, but you could also do that in a loop, and use grep() to extract those numbers ending in a particular value:



                        yourlist <- vector("list", 10)

                        for (i in 1:10)
                        yourlist[[i]] <- grep(paste0(i %% 10, "$"), 1:100)






                        share|improve this answer



























                          0














                          This is uglier than joran's answer, but you could also do that in a loop, and use grep() to extract those numbers ending in a particular value:



                          yourlist <- vector("list", 10)

                          for (i in 1:10)
                          yourlist[[i]] <- grep(paste0(i %% 10, "$"), 1:100)






                          share|improve this answer

























                            0












                            0








                            0







                            This is uglier than joran's answer, but you could also do that in a loop, and use grep() to extract those numbers ending in a particular value:



                            yourlist <- vector("list", 10)

                            for (i in 1:10)
                            yourlist[[i]] <- grep(paste0(i %% 10, "$"), 1:100)






                            share|improve this answer













                            This is uglier than joran's answer, but you could also do that in a loop, and use grep() to extract those numbers ending in a particular value:



                            yourlist <- vector("list", 10)

                            for (i in 1:10)
                            yourlist[[i]] <- grep(paste0(i %% 10, "$"), 1:100)







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 '18 at 21:19









                            PhilPhil

                            1,863629




                            1,863629



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308555%2fsplit-vector-at-multiplicity-of-a-number%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                Syphilis

                                Darth Vader #20