Joining nested lists and adding column











up vote
0
down vote

favorite












I have a list containing two matrices:



a <- list("m1"=matrix(1:9, nrow = 3, ncol = 3),
"m2"=matrix(1:9, nrow = 3, ncol = 3))


I want to bind the two matrices (row-bind), and to distinguish the rows, I want to add a column that contains the name of the matrix. I can bind the rows using r bind:



b <- do.call(rbind, a) %>% as.data.frame


which yields



  V1 V2 V3
1 1 4 7
2 2 5 8
3 3 6 9
4 1 4 7
5 2 5 8
6 3 6 9


But how do I add a column containing the names? I can do b$id <- c("m1","m1","m1","m2","m2","m2"), but there must be an easier way than this (?)










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a list containing two matrices:



    a <- list("m1"=matrix(1:9, nrow = 3, ncol = 3),
    "m2"=matrix(1:9, nrow = 3, ncol = 3))


    I want to bind the two matrices (row-bind), and to distinguish the rows, I want to add a column that contains the name of the matrix. I can bind the rows using r bind:



    b <- do.call(rbind, a) %>% as.data.frame


    which yields



      V1 V2 V3
    1 1 4 7
    2 2 5 8
    3 3 6 9
    4 1 4 7
    5 2 5 8
    6 3 6 9


    But how do I add a column containing the names? I can do b$id <- c("m1","m1","m1","m2","m2","m2"), but there must be an easier way than this (?)










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a list containing two matrices:



      a <- list("m1"=matrix(1:9, nrow = 3, ncol = 3),
      "m2"=matrix(1:9, nrow = 3, ncol = 3))


      I want to bind the two matrices (row-bind), and to distinguish the rows, I want to add a column that contains the name of the matrix. I can bind the rows using r bind:



      b <- do.call(rbind, a) %>% as.data.frame


      which yields



        V1 V2 V3
      1 1 4 7
      2 2 5 8
      3 3 6 9
      4 1 4 7
      5 2 5 8
      6 3 6 9


      But how do I add a column containing the names? I can do b$id <- c("m1","m1","m1","m2","m2","m2"), but there must be an easier way than this (?)










      share|improve this question













      I have a list containing two matrices:



      a <- list("m1"=matrix(1:9, nrow = 3, ncol = 3),
      "m2"=matrix(1:9, nrow = 3, ncol = 3))


      I want to bind the two matrices (row-bind), and to distinguish the rows, I want to add a column that contains the name of the matrix. I can bind the rows using r bind:



      b <- do.call(rbind, a) %>% as.data.frame


      which yields



        V1 V2 V3
      1 1 4 7
      2 2 5 8
      3 3 6 9
      4 1 4 7
      5 2 5 8
      6 3 6 9


      But how do I add a column containing the names? I can do b$id <- c("m1","m1","m1","m2","m2","m2"), but there must be an easier way than this (?)







      r dplyr






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 11:28









      N08

      48818




      48818
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          Here's how to do it in dplyr / purrr



          a %>% purrr::map(as.data.frame) %>% dplyr::bind_rows(.id = "origin")
          origin V1 V2 V3
          1 m1 1 4 7
          2 m1 2 5 8
          3 m1 3 6 9
          4 m2 1 4 7
          5 m2 2 5 8
          6 m2 3 6 9


          That converts the matrices to data-frames before row-binding them.



          You can use bind_rows on a list of matrices. But it doesn't return what you expect.



          a %>% bind_rows(.id = "origin")
          # A tibble: 9 x 3
          origin m1 m2
          <chr> <int> <int>
          1 1 1 1
          2 1 2 2
          3 1 3 3
          4 1 4 4
          5 1 5 5
          6 1 6 6
          7 1 7 7
          8 1 8 8
          9 1 9 9


          This happens because m1 and m2 are vectors (because they are matrices) of the same length, and bind_rows sees a list of constant-length vectors as a single data-frame. So the latter call is equivalent to



          bind_rows(data.frame(m1 = as.vector(m1), m2 = as.vector(m2)), .id = "origin")


          So, make sure you convert your matrices to data.frames before you bind them together.






          share|improve this answer






























            up vote
            0
            down vote













            You can do:



            b <- do.call(rbind.data.frame, a)

            # V1 V2 V3
            #m1.1 1 4 7
            #m1.2 2 5 8
            #m1.3 3 6 9
            #m2.1 1 4 7
            #m2.2 2 5 8
            #m2.3 3 6 9


            or if you not happy with this,



            b    <- do.call(rbind.data.frame, a)
            b$id <- sub("[.].+", "", rownames(b))

            # V1 V2 V3 id
            #m1.1 1 4 7 m1
            #m1.2 2 5 8 m1
            #m1.3 3 6 9 m1
            #m2.1 1 4 7 m2
            #m2.2 2 5 8 m2
            #m2.3 3 6 9 m2





            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',
              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%2f53206836%2fjoining-nested-lists-and-adding-column%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








              up vote
              2
              down vote













              Here's how to do it in dplyr / purrr



              a %>% purrr::map(as.data.frame) %>% dplyr::bind_rows(.id = "origin")
              origin V1 V2 V3
              1 m1 1 4 7
              2 m1 2 5 8
              3 m1 3 6 9
              4 m2 1 4 7
              5 m2 2 5 8
              6 m2 3 6 9


              That converts the matrices to data-frames before row-binding them.



              You can use bind_rows on a list of matrices. But it doesn't return what you expect.



              a %>% bind_rows(.id = "origin")
              # A tibble: 9 x 3
              origin m1 m2
              <chr> <int> <int>
              1 1 1 1
              2 1 2 2
              3 1 3 3
              4 1 4 4
              5 1 5 5
              6 1 6 6
              7 1 7 7
              8 1 8 8
              9 1 9 9


              This happens because m1 and m2 are vectors (because they are matrices) of the same length, and bind_rows sees a list of constant-length vectors as a single data-frame. So the latter call is equivalent to



              bind_rows(data.frame(m1 = as.vector(m1), m2 = as.vector(m2)), .id = "origin")


              So, make sure you convert your matrices to data.frames before you bind them together.






              share|improve this answer



























                up vote
                2
                down vote













                Here's how to do it in dplyr / purrr



                a %>% purrr::map(as.data.frame) %>% dplyr::bind_rows(.id = "origin")
                origin V1 V2 V3
                1 m1 1 4 7
                2 m1 2 5 8
                3 m1 3 6 9
                4 m2 1 4 7
                5 m2 2 5 8
                6 m2 3 6 9


                That converts the matrices to data-frames before row-binding them.



                You can use bind_rows on a list of matrices. But it doesn't return what you expect.



                a %>% bind_rows(.id = "origin")
                # A tibble: 9 x 3
                origin m1 m2
                <chr> <int> <int>
                1 1 1 1
                2 1 2 2
                3 1 3 3
                4 1 4 4
                5 1 5 5
                6 1 6 6
                7 1 7 7
                8 1 8 8
                9 1 9 9


                This happens because m1 and m2 are vectors (because they are matrices) of the same length, and bind_rows sees a list of constant-length vectors as a single data-frame. So the latter call is equivalent to



                bind_rows(data.frame(m1 = as.vector(m1), m2 = as.vector(m2)), .id = "origin")


                So, make sure you convert your matrices to data.frames before you bind them together.






                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Here's how to do it in dplyr / purrr



                  a %>% purrr::map(as.data.frame) %>% dplyr::bind_rows(.id = "origin")
                  origin V1 V2 V3
                  1 m1 1 4 7
                  2 m1 2 5 8
                  3 m1 3 6 9
                  4 m2 1 4 7
                  5 m2 2 5 8
                  6 m2 3 6 9


                  That converts the matrices to data-frames before row-binding them.



                  You can use bind_rows on a list of matrices. But it doesn't return what you expect.



                  a %>% bind_rows(.id = "origin")
                  # A tibble: 9 x 3
                  origin m1 m2
                  <chr> <int> <int>
                  1 1 1 1
                  2 1 2 2
                  3 1 3 3
                  4 1 4 4
                  5 1 5 5
                  6 1 6 6
                  7 1 7 7
                  8 1 8 8
                  9 1 9 9


                  This happens because m1 and m2 are vectors (because they are matrices) of the same length, and bind_rows sees a list of constant-length vectors as a single data-frame. So the latter call is equivalent to



                  bind_rows(data.frame(m1 = as.vector(m1), m2 = as.vector(m2)), .id = "origin")


                  So, make sure you convert your matrices to data.frames before you bind them together.






                  share|improve this answer














                  Here's how to do it in dplyr / purrr



                  a %>% purrr::map(as.data.frame) %>% dplyr::bind_rows(.id = "origin")
                  origin V1 V2 V3
                  1 m1 1 4 7
                  2 m1 2 5 8
                  3 m1 3 6 9
                  4 m2 1 4 7
                  5 m2 2 5 8
                  6 m2 3 6 9


                  That converts the matrices to data-frames before row-binding them.



                  You can use bind_rows on a list of matrices. But it doesn't return what you expect.



                  a %>% bind_rows(.id = "origin")
                  # A tibble: 9 x 3
                  origin m1 m2
                  <chr> <int> <int>
                  1 1 1 1
                  2 1 2 2
                  3 1 3 3
                  4 1 4 4
                  5 1 5 5
                  6 1 6 6
                  7 1 7 7
                  8 1 8 8
                  9 1 9 9


                  This happens because m1 and m2 are vectors (because they are matrices) of the same length, and bind_rows sees a list of constant-length vectors as a single data-frame. So the latter call is equivalent to



                  bind_rows(data.frame(m1 = as.vector(m1), m2 = as.vector(m2)), .id = "origin")


                  So, make sure you convert your matrices to data.frames before you bind them together.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 8 at 11:45

























                  answered Nov 8 at 11:36









                  Russ Hyde

                  901513




                  901513
























                      up vote
                      0
                      down vote













                      You can do:



                      b <- do.call(rbind.data.frame, a)

                      # V1 V2 V3
                      #m1.1 1 4 7
                      #m1.2 2 5 8
                      #m1.3 3 6 9
                      #m2.1 1 4 7
                      #m2.2 2 5 8
                      #m2.3 3 6 9


                      or if you not happy with this,



                      b    <- do.call(rbind.data.frame, a)
                      b$id <- sub("[.].+", "", rownames(b))

                      # V1 V2 V3 id
                      #m1.1 1 4 7 m1
                      #m1.2 2 5 8 m1
                      #m1.3 3 6 9 m1
                      #m2.1 1 4 7 m2
                      #m2.2 2 5 8 m2
                      #m2.3 3 6 9 m2





                      share|improve this answer



























                        up vote
                        0
                        down vote













                        You can do:



                        b <- do.call(rbind.data.frame, a)

                        # V1 V2 V3
                        #m1.1 1 4 7
                        #m1.2 2 5 8
                        #m1.3 3 6 9
                        #m2.1 1 4 7
                        #m2.2 2 5 8
                        #m2.3 3 6 9


                        or if you not happy with this,



                        b    <- do.call(rbind.data.frame, a)
                        b$id <- sub("[.].+", "", rownames(b))

                        # V1 V2 V3 id
                        #m1.1 1 4 7 m1
                        #m1.2 2 5 8 m1
                        #m1.3 3 6 9 m1
                        #m2.1 1 4 7 m2
                        #m2.2 2 5 8 m2
                        #m2.3 3 6 9 m2





                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You can do:



                          b <- do.call(rbind.data.frame, a)

                          # V1 V2 V3
                          #m1.1 1 4 7
                          #m1.2 2 5 8
                          #m1.3 3 6 9
                          #m2.1 1 4 7
                          #m2.2 2 5 8
                          #m2.3 3 6 9


                          or if you not happy with this,



                          b    <- do.call(rbind.data.frame, a)
                          b$id <- sub("[.].+", "", rownames(b))

                          # V1 V2 V3 id
                          #m1.1 1 4 7 m1
                          #m1.2 2 5 8 m1
                          #m1.3 3 6 9 m1
                          #m2.1 1 4 7 m2
                          #m2.2 2 5 8 m2
                          #m2.3 3 6 9 m2





                          share|improve this answer














                          You can do:



                          b <- do.call(rbind.data.frame, a)

                          # V1 V2 V3
                          #m1.1 1 4 7
                          #m1.2 2 5 8
                          #m1.3 3 6 9
                          #m2.1 1 4 7
                          #m2.2 2 5 8
                          #m2.3 3 6 9


                          or if you not happy with this,



                          b    <- do.call(rbind.data.frame, a)
                          b$id <- sub("[.].+", "", rownames(b))

                          # V1 V2 V3 id
                          #m1.1 1 4 7 m1
                          #m1.2 2 5 8 m1
                          #m1.3 3 6 9 m1
                          #m2.1 1 4 7 m2
                          #m2.2 2 5 8 m2
                          #m2.3 3 6 9 m2






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 8 at 11:51

























                          answered Nov 8 at 11:37









                          Andre Elrico

                          4,6971827




                          4,6971827






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206836%2fjoining-nested-lists-and-adding-column%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

                              Schultheiß

                              Verwaltungsgliederung Dänemarks

                              Liste der Kulturdenkmale in Wilsdruff