Convert Vector to Matrix without Recycling












4















When I convert a vector to a matrix that has too few elements to fill the matrix, then the elements of the vector are recycled. Is there any way to turn recycling off or otherwise replace the recycled elements with NA?



This is the default behavior:



> matrix(c(1,2,3,4,5,6,7,8,9,10,11),ncol=2,byrow=TRUE)
[,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
[5,] 9 10
[6,] 11 1
Warning message:
In matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), ncol = 2, byrow = TRUE) :
data length [11] is not a sub-multiple or multiple of the number of rows [6]


I would like the resulting matrix to be



     [,1] [,2]
[1,] 1 2
[2,] 3 4
[3,] 5 6
[4,] 7 8
[5,] 9 10
[6,] 11 NA









share|improve this question





























    4















    When I convert a vector to a matrix that has too few elements to fill the matrix, then the elements of the vector are recycled. Is there any way to turn recycling off or otherwise replace the recycled elements with NA?



    This is the default behavior:



    > matrix(c(1,2,3,4,5,6,7,8,9,10,11),ncol=2,byrow=TRUE)
    [,1] [,2]
    [1,] 1 2
    [2,] 3 4
    [3,] 5 6
    [4,] 7 8
    [5,] 9 10
    [6,] 11 1
    Warning message:
    In matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), ncol = 2, byrow = TRUE) :
    data length [11] is not a sub-multiple or multiple of the number of rows [6]


    I would like the resulting matrix to be



         [,1] [,2]
    [1,] 1 2
    [2,] 3 4
    [3,] 5 6
    [4,] 7 8
    [5,] 9 10
    [6,] 11 NA









    share|improve this question



























      4












      4








      4


      1






      When I convert a vector to a matrix that has too few elements to fill the matrix, then the elements of the vector are recycled. Is there any way to turn recycling off or otherwise replace the recycled elements with NA?



      This is the default behavior:



      > matrix(c(1,2,3,4,5,6,7,8,9,10,11),ncol=2,byrow=TRUE)
      [,1] [,2]
      [1,] 1 2
      [2,] 3 4
      [3,] 5 6
      [4,] 7 8
      [5,] 9 10
      [6,] 11 1
      Warning message:
      In matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), ncol = 2, byrow = TRUE) :
      data length [11] is not a sub-multiple or multiple of the number of rows [6]


      I would like the resulting matrix to be



           [,1] [,2]
      [1,] 1 2
      [2,] 3 4
      [3,] 5 6
      [4,] 7 8
      [5,] 9 10
      [6,] 11 NA









      share|improve this question
















      When I convert a vector to a matrix that has too few elements to fill the matrix, then the elements of the vector are recycled. Is there any way to turn recycling off or otherwise replace the recycled elements with NA?



      This is the default behavior:



      > matrix(c(1,2,3,4,5,6,7,8,9,10,11),ncol=2,byrow=TRUE)
      [,1] [,2]
      [1,] 1 2
      [2,] 3 4
      [3,] 5 6
      [4,] 7 8
      [5,] 9 10
      [6,] 11 1
      Warning message:
      In matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11), ncol = 2, byrow = TRUE) :
      data length [11] is not a sub-multiple or multiple of the number of rows [6]


      I would like the resulting matrix to be



           [,1] [,2]
      [1,] 1 2
      [2,] 3 4
      [3,] 5 6
      [4,] 7 8
      [5,] 9 10
      [6,] 11 NA






      r matrix vector






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 8:43









      Jaap

      57.3k21124137




      57.3k21124137










      asked Apr 30 '15 at 2:15









      David DicksonDavid Dickson

      342415




      342415
























          2 Answers
          2






          active

          oldest

          votes


















          9














          You can't turn recycling off, but you can do some manipulations to the vector before you form the matrix. We can extend the length of the vector based on what the dimensions of the matrix will be. The length<- replacement function will pad the vector with NA up to the desired length.



          x <- 1:11
          length(x) <- prod(dim(matrix(x, ncol = 2)))
          ## you will get a warning here unless suppressWarnings() is used
          matrix(x, ncol = 2, byrow = TRUE)
          # [,1] [,2]
          # [1,] 1 2
          # [2,] 3 4
          # [3,] 5 6
          # [4,] 7 8
          # [5,] 9 10
          # [6,] 11 NA





          share|improve this answer


























          • Very simple solution, thank you!

            – David Dickson
            Apr 30 '15 at 3:30



















          2














          This will create a matrix with nr rows containing x with NAs at the end without any warnings.



          # inputs
          nr <- 2
          x <- 1:11

          nc <- ceiling(length(x) / nr)


          and then any of the following:



          t(replace(matrix(NA, nc, nr), seq_along(x), x))

          matrix(`length<-`(x, nr * nc), nr, byrow = TRUE)

          matrix(c(x, rep(NA, nr * nc - length(x))), nr, byrow = TRUE)

          matrix(replace(rep(NA, nr * nc), seq_along(x), x), nr, byrow = TRUE)


          To fill the matrix by column use this in place of the first alternative and omit byrow=TRUE for the remaining alternatives.



          replace(matrix(NA, nr, nc), seq_along(x), x)





          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%2f29957893%2fconvert-vector-to-matrix-without-recycling%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            9














            You can't turn recycling off, but you can do some manipulations to the vector before you form the matrix. We can extend the length of the vector based on what the dimensions of the matrix will be. The length<- replacement function will pad the vector with NA up to the desired length.



            x <- 1:11
            length(x) <- prod(dim(matrix(x, ncol = 2)))
            ## you will get a warning here unless suppressWarnings() is used
            matrix(x, ncol = 2, byrow = TRUE)
            # [,1] [,2]
            # [1,] 1 2
            # [2,] 3 4
            # [3,] 5 6
            # [4,] 7 8
            # [5,] 9 10
            # [6,] 11 NA





            share|improve this answer


























            • Very simple solution, thank you!

              – David Dickson
              Apr 30 '15 at 3:30
















            9














            You can't turn recycling off, but you can do some manipulations to the vector before you form the matrix. We can extend the length of the vector based on what the dimensions of the matrix will be. The length<- replacement function will pad the vector with NA up to the desired length.



            x <- 1:11
            length(x) <- prod(dim(matrix(x, ncol = 2)))
            ## you will get a warning here unless suppressWarnings() is used
            matrix(x, ncol = 2, byrow = TRUE)
            # [,1] [,2]
            # [1,] 1 2
            # [2,] 3 4
            # [3,] 5 6
            # [4,] 7 8
            # [5,] 9 10
            # [6,] 11 NA





            share|improve this answer


























            • Very simple solution, thank you!

              – David Dickson
              Apr 30 '15 at 3:30














            9












            9








            9







            You can't turn recycling off, but you can do some manipulations to the vector before you form the matrix. We can extend the length of the vector based on what the dimensions of the matrix will be. The length<- replacement function will pad the vector with NA up to the desired length.



            x <- 1:11
            length(x) <- prod(dim(matrix(x, ncol = 2)))
            ## you will get a warning here unless suppressWarnings() is used
            matrix(x, ncol = 2, byrow = TRUE)
            # [,1] [,2]
            # [1,] 1 2
            # [2,] 3 4
            # [3,] 5 6
            # [4,] 7 8
            # [5,] 9 10
            # [6,] 11 NA





            share|improve this answer















            You can't turn recycling off, but you can do some manipulations to the vector before you form the matrix. We can extend the length of the vector based on what the dimensions of the matrix will be. The length<- replacement function will pad the vector with NA up to the desired length.



            x <- 1:11
            length(x) <- prod(dim(matrix(x, ncol = 2)))
            ## you will get a warning here unless suppressWarnings() is used
            matrix(x, ncol = 2, byrow = TRUE)
            # [,1] [,2]
            # [1,] 1 2
            # [2,] 3 4
            # [3,] 5 6
            # [4,] 7 8
            # [5,] 9 10
            # [6,] 11 NA






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 30 '15 at 2:32

























            answered Apr 30 '15 at 2:20









            Rich ScrivenRich Scriven

            77.5k8104173




            77.5k8104173













            • Very simple solution, thank you!

              – David Dickson
              Apr 30 '15 at 3:30



















            • Very simple solution, thank you!

              – David Dickson
              Apr 30 '15 at 3:30

















            Very simple solution, thank you!

            – David Dickson
            Apr 30 '15 at 3:30





            Very simple solution, thank you!

            – David Dickson
            Apr 30 '15 at 3:30













            2














            This will create a matrix with nr rows containing x with NAs at the end without any warnings.



            # inputs
            nr <- 2
            x <- 1:11

            nc <- ceiling(length(x) / nr)


            and then any of the following:



            t(replace(matrix(NA, nc, nr), seq_along(x), x))

            matrix(`length<-`(x, nr * nc), nr, byrow = TRUE)

            matrix(c(x, rep(NA, nr * nc - length(x))), nr, byrow = TRUE)

            matrix(replace(rep(NA, nr * nc), seq_along(x), x), nr, byrow = TRUE)


            To fill the matrix by column use this in place of the first alternative and omit byrow=TRUE for the remaining alternatives.



            replace(matrix(NA, nr, nc), seq_along(x), x)





            share|improve this answer






























              2














              This will create a matrix with nr rows containing x with NAs at the end without any warnings.



              # inputs
              nr <- 2
              x <- 1:11

              nc <- ceiling(length(x) / nr)


              and then any of the following:



              t(replace(matrix(NA, nc, nr), seq_along(x), x))

              matrix(`length<-`(x, nr * nc), nr, byrow = TRUE)

              matrix(c(x, rep(NA, nr * nc - length(x))), nr, byrow = TRUE)

              matrix(replace(rep(NA, nr * nc), seq_along(x), x), nr, byrow = TRUE)


              To fill the matrix by column use this in place of the first alternative and omit byrow=TRUE for the remaining alternatives.



              replace(matrix(NA, nr, nc), seq_along(x), x)





              share|improve this answer




























                2












                2








                2







                This will create a matrix with nr rows containing x with NAs at the end without any warnings.



                # inputs
                nr <- 2
                x <- 1:11

                nc <- ceiling(length(x) / nr)


                and then any of the following:



                t(replace(matrix(NA, nc, nr), seq_along(x), x))

                matrix(`length<-`(x, nr * nc), nr, byrow = TRUE)

                matrix(c(x, rep(NA, nr * nc - length(x))), nr, byrow = TRUE)

                matrix(replace(rep(NA, nr * nc), seq_along(x), x), nr, byrow = TRUE)


                To fill the matrix by column use this in place of the first alternative and omit byrow=TRUE for the remaining alternatives.



                replace(matrix(NA, nr, nc), seq_along(x), x)





                share|improve this answer















                This will create a matrix with nr rows containing x with NAs at the end without any warnings.



                # inputs
                nr <- 2
                x <- 1:11

                nc <- ceiling(length(x) / nr)


                and then any of the following:



                t(replace(matrix(NA, nc, nr), seq_along(x), x))

                matrix(`length<-`(x, nr * nc), nr, byrow = TRUE)

                matrix(c(x, rep(NA, nr * nc - length(x))), nr, byrow = TRUE)

                matrix(replace(rep(NA, nr * nc), seq_along(x), x), nr, byrow = TRUE)


                To fill the matrix by column use this in place of the first alternative and omit byrow=TRUE for the remaining alternatives.



                replace(matrix(NA, nr, nc), seq_along(x), x)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 4 '18 at 11:36

























                answered Aug 4 '18 at 11:21









                G. GrothendieckG. Grothendieck

                153k10136244




                153k10136244






























                    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%2f29957893%2fconvert-vector-to-matrix-without-recycling%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

                    Bressuire

                    Vorschmack

                    Quarantine