How to combine frequency tables with missing values?











up vote
1
down vote

favorite












I have the following list of tables:



    list(structure(c(`0` = 19L, `1` = 2L, `3` = 43L), .Dim = 3L, .Dimnames = structure(list(
c("0", "1", "3")), .Names = ""), class = "table"), structure(c(`0` = 7L,
`1` = 9L, `2` = 5L, `3` = 43L), .Dim = 4L, .Dimnames = structure(list(
c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 14L,
`1` = 2L, `2` = 4L, `3` = 44L), .Dim = 4L, .Dimnames = structure(list(
c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 21L,
`1` = 8L, `2` = 2L, `3` = 33L), .Dim = 4L, .Dimnames = structure(list(
c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 23L,
`1` = 3L, `2` = 1L, `3` = 37L), .Dim = 4L, .Dimnames = structure(list(
c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 19L,
`1` = 2L, `2` = 4L, `3` = 39L), .Dim = 4L, .Dimnames = structure(list(
c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 22L,
`1` = 1L, `2` = 4L, `3` = 37L), .Dim = 4L, .Dimnames = structure(list(
c("0", "1", "2", "3")), .Names = ""), class = "table"))


Each table is a tally of observations of values 0, 1, 2, or 3. However, not all values are represented in all tables, so some of the tables have missing columns. I would like those missing values to be assigned 0 in the final output.



merge doesn't work well on lists, and lapplying rbind doesn't work because not all the tables have matching columns.



How can I combine these tables into a single matrix or data.frame with one column for each value (0, 1, 2, 3) and one row for each count (7 in this example)?



The final output should look like this:



structure(list(`0` = c(19L, 7L, 14L, 21L, 23L, 19L, 22L), `1` = c(2L, 
9L, 2L, 8L, 3L, 2L, 1L), `2` = c(0L, 5L, 4L, 2L, 1L, 4L, 4L),
`3` = c(43L, 43L, 44L, 33L, 37L, 39L, 37L)), class = "data.frame", row.names = c(NA,
-7L))









share|improve this question


























    up vote
    1
    down vote

    favorite












    I have the following list of tables:



        list(structure(c(`0` = 19L, `1` = 2L, `3` = 43L), .Dim = 3L, .Dimnames = structure(list(
    c("0", "1", "3")), .Names = ""), class = "table"), structure(c(`0` = 7L,
    `1` = 9L, `2` = 5L, `3` = 43L), .Dim = 4L, .Dimnames = structure(list(
    c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 14L,
    `1` = 2L, `2` = 4L, `3` = 44L), .Dim = 4L, .Dimnames = structure(list(
    c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 21L,
    `1` = 8L, `2` = 2L, `3` = 33L), .Dim = 4L, .Dimnames = structure(list(
    c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 23L,
    `1` = 3L, `2` = 1L, `3` = 37L), .Dim = 4L, .Dimnames = structure(list(
    c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 19L,
    `1` = 2L, `2` = 4L, `3` = 39L), .Dim = 4L, .Dimnames = structure(list(
    c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 22L,
    `1` = 1L, `2` = 4L, `3` = 37L), .Dim = 4L, .Dimnames = structure(list(
    c("0", "1", "2", "3")), .Names = ""), class = "table"))


    Each table is a tally of observations of values 0, 1, 2, or 3. However, not all values are represented in all tables, so some of the tables have missing columns. I would like those missing values to be assigned 0 in the final output.



    merge doesn't work well on lists, and lapplying rbind doesn't work because not all the tables have matching columns.



    How can I combine these tables into a single matrix or data.frame with one column for each value (0, 1, 2, 3) and one row for each count (7 in this example)?



    The final output should look like this:



    structure(list(`0` = c(19L, 7L, 14L, 21L, 23L, 19L, 22L), `1` = c(2L, 
    9L, 2L, 8L, 3L, 2L, 1L), `2` = c(0L, 5L, 4L, 2L, 1L, 4L, 4L),
    `3` = c(43L, 43L, 44L, 33L, 37L, 39L, 37L)), class = "data.frame", row.names = c(NA,
    -7L))









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have the following list of tables:



          list(structure(c(`0` = 19L, `1` = 2L, `3` = 43L), .Dim = 3L, .Dimnames = structure(list(
      c("0", "1", "3")), .Names = ""), class = "table"), structure(c(`0` = 7L,
      `1` = 9L, `2` = 5L, `3` = 43L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 14L,
      `1` = 2L, `2` = 4L, `3` = 44L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 21L,
      `1` = 8L, `2` = 2L, `3` = 33L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 23L,
      `1` = 3L, `2` = 1L, `3` = 37L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 19L,
      `1` = 2L, `2` = 4L, `3` = 39L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 22L,
      `1` = 1L, `2` = 4L, `3` = 37L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"))


      Each table is a tally of observations of values 0, 1, 2, or 3. However, not all values are represented in all tables, so some of the tables have missing columns. I would like those missing values to be assigned 0 in the final output.



      merge doesn't work well on lists, and lapplying rbind doesn't work because not all the tables have matching columns.



      How can I combine these tables into a single matrix or data.frame with one column for each value (0, 1, 2, 3) and one row for each count (7 in this example)?



      The final output should look like this:



      structure(list(`0` = c(19L, 7L, 14L, 21L, 23L, 19L, 22L), `1` = c(2L, 
      9L, 2L, 8L, 3L, 2L, 1L), `2` = c(0L, 5L, 4L, 2L, 1L, 4L, 4L),
      `3` = c(43L, 43L, 44L, 33L, 37L, 39L, 37L)), class = "data.frame", row.names = c(NA,
      -7L))









      share|improve this question













      I have the following list of tables:



          list(structure(c(`0` = 19L, `1` = 2L, `3` = 43L), .Dim = 3L, .Dimnames = structure(list(
      c("0", "1", "3")), .Names = ""), class = "table"), structure(c(`0` = 7L,
      `1` = 9L, `2` = 5L, `3` = 43L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 14L,
      `1` = 2L, `2` = 4L, `3` = 44L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 21L,
      `1` = 8L, `2` = 2L, `3` = 33L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 23L,
      `1` = 3L, `2` = 1L, `3` = 37L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 19L,
      `1` = 2L, `2` = 4L, `3` = 39L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"), structure(c(`0` = 22L,
      `1` = 1L, `2` = 4L, `3` = 37L), .Dim = 4L, .Dimnames = structure(list(
      c("0", "1", "2", "3")), .Names = ""), class = "table"))


      Each table is a tally of observations of values 0, 1, 2, or 3. However, not all values are represented in all tables, so some of the tables have missing columns. I would like those missing values to be assigned 0 in the final output.



      merge doesn't work well on lists, and lapplying rbind doesn't work because not all the tables have matching columns.



      How can I combine these tables into a single matrix or data.frame with one column for each value (0, 1, 2, 3) and one row for each count (7 in this example)?



      The final output should look like this:



      structure(list(`0` = c(19L, 7L, 14L, 21L, 23L, 19L, 22L), `1` = c(2L, 
      9L, 2L, 8L, 3L, 2L, 1L), `2` = c(0L, 5L, 4L, 2L, 1L, 4L, 4L),
      `3` = c(43L, 43L, 44L, 33L, 37L, 39L, 37L)), class = "data.frame", row.names = c(NA,
      -7L))






      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 18:03









      Matt

      5661521




      5661521
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          We convert the individual datasets to data.frame with map and use bind_rows to row bind the datasets to a single dataset



          library(tidyverse)
          map(lst, as.data.frame.list, check.names = FALSE) %>%
          bind_rows





          share|improve this answer






























            up vote
            1
            down vote













            In base R and assuming your list is named mylist, then you can do something like the following.



            all_names <- sort(unique(unlist(lapply(mylist, names))))

            res <- do.call("rbind", lapply(mylist, function(x) x[all_names]))
            print(res)
            # 0 1 <NA> 3
            #[1,] 19 2 NA 43
            #[2,] 7 9 5 43
            #[3,] 14 2 4 44
            #[4,] 21 8 2 33
            #[5,] 23 3 1 37
            #[6,] 19 2 4 39
            #[7,] 22 1 4 37


            Now, you can either accept that or do a few edits to get it perfect:



            colnames(res) <- all_names  # Ensure correct colnames
            res[is.na(res)] <- 0 # Overwrite NAs with 0
            print(res)
            # 0 1 2 3
            #[1,] 19 2 0 43
            #[2,] 7 9 5 43
            #[3,] 14 2 4 44
            #[4,] 21 8 2 33
            #[5,] 23 3 1 37
            #[6,] 19 2 4 39
            #[7,] 22 1 4 37





            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%2f53195214%2fhow-to-combine-frequency-tables-with-missing-values%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
              1
              down vote



              accepted










              We convert the individual datasets to data.frame with map and use bind_rows to row bind the datasets to a single dataset



              library(tidyverse)
              map(lst, as.data.frame.list, check.names = FALSE) %>%
              bind_rows





              share|improve this answer



























                up vote
                1
                down vote



                accepted










                We convert the individual datasets to data.frame with map and use bind_rows to row bind the datasets to a single dataset



                library(tidyverse)
                map(lst, as.data.frame.list, check.names = FALSE) %>%
                bind_rows





                share|improve this answer

























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  We convert the individual datasets to data.frame with map and use bind_rows to row bind the datasets to a single dataset



                  library(tidyverse)
                  map(lst, as.data.frame.list, check.names = FALSE) %>%
                  bind_rows





                  share|improve this answer














                  We convert the individual datasets to data.frame with map and use bind_rows to row bind the datasets to a single dataset



                  library(tidyverse)
                  map(lst, as.data.frame.list, check.names = FALSE) %>%
                  bind_rows






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 7 at 18:19

























                  answered Nov 7 at 18:05









                  akrun

                  390k13178251




                  390k13178251
























                      up vote
                      1
                      down vote













                      In base R and assuming your list is named mylist, then you can do something like the following.



                      all_names <- sort(unique(unlist(lapply(mylist, names))))

                      res <- do.call("rbind", lapply(mylist, function(x) x[all_names]))
                      print(res)
                      # 0 1 <NA> 3
                      #[1,] 19 2 NA 43
                      #[2,] 7 9 5 43
                      #[3,] 14 2 4 44
                      #[4,] 21 8 2 33
                      #[5,] 23 3 1 37
                      #[6,] 19 2 4 39
                      #[7,] 22 1 4 37


                      Now, you can either accept that or do a few edits to get it perfect:



                      colnames(res) <- all_names  # Ensure correct colnames
                      res[is.na(res)] <- 0 # Overwrite NAs with 0
                      print(res)
                      # 0 1 2 3
                      #[1,] 19 2 0 43
                      #[2,] 7 9 5 43
                      #[3,] 14 2 4 44
                      #[4,] 21 8 2 33
                      #[5,] 23 3 1 37
                      #[6,] 19 2 4 39
                      #[7,] 22 1 4 37





                      share|improve this answer

























                        up vote
                        1
                        down vote













                        In base R and assuming your list is named mylist, then you can do something like the following.



                        all_names <- sort(unique(unlist(lapply(mylist, names))))

                        res <- do.call("rbind", lapply(mylist, function(x) x[all_names]))
                        print(res)
                        # 0 1 <NA> 3
                        #[1,] 19 2 NA 43
                        #[2,] 7 9 5 43
                        #[3,] 14 2 4 44
                        #[4,] 21 8 2 33
                        #[5,] 23 3 1 37
                        #[6,] 19 2 4 39
                        #[7,] 22 1 4 37


                        Now, you can either accept that or do a few edits to get it perfect:



                        colnames(res) <- all_names  # Ensure correct colnames
                        res[is.na(res)] <- 0 # Overwrite NAs with 0
                        print(res)
                        # 0 1 2 3
                        #[1,] 19 2 0 43
                        #[2,] 7 9 5 43
                        #[3,] 14 2 4 44
                        #[4,] 21 8 2 33
                        #[5,] 23 3 1 37
                        #[6,] 19 2 4 39
                        #[7,] 22 1 4 37





                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          In base R and assuming your list is named mylist, then you can do something like the following.



                          all_names <- sort(unique(unlist(lapply(mylist, names))))

                          res <- do.call("rbind", lapply(mylist, function(x) x[all_names]))
                          print(res)
                          # 0 1 <NA> 3
                          #[1,] 19 2 NA 43
                          #[2,] 7 9 5 43
                          #[3,] 14 2 4 44
                          #[4,] 21 8 2 33
                          #[5,] 23 3 1 37
                          #[6,] 19 2 4 39
                          #[7,] 22 1 4 37


                          Now, you can either accept that or do a few edits to get it perfect:



                          colnames(res) <- all_names  # Ensure correct colnames
                          res[is.na(res)] <- 0 # Overwrite NAs with 0
                          print(res)
                          # 0 1 2 3
                          #[1,] 19 2 0 43
                          #[2,] 7 9 5 43
                          #[3,] 14 2 4 44
                          #[4,] 21 8 2 33
                          #[5,] 23 3 1 37
                          #[6,] 19 2 4 39
                          #[7,] 22 1 4 37





                          share|improve this answer












                          In base R and assuming your list is named mylist, then you can do something like the following.



                          all_names <- sort(unique(unlist(lapply(mylist, names))))

                          res <- do.call("rbind", lapply(mylist, function(x) x[all_names]))
                          print(res)
                          # 0 1 <NA> 3
                          #[1,] 19 2 NA 43
                          #[2,] 7 9 5 43
                          #[3,] 14 2 4 44
                          #[4,] 21 8 2 33
                          #[5,] 23 3 1 37
                          #[6,] 19 2 4 39
                          #[7,] 22 1 4 37


                          Now, you can either accept that or do a few edits to get it perfect:



                          colnames(res) <- all_names  # Ensure correct colnames
                          res[is.na(res)] <- 0 # Overwrite NAs with 0
                          print(res)
                          # 0 1 2 3
                          #[1,] 19 2 0 43
                          #[2,] 7 9 5 43
                          #[3,] 14 2 4 44
                          #[4,] 21 8 2 33
                          #[5,] 23 3 1 37
                          #[6,] 19 2 4 39
                          #[7,] 22 1 4 37






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 7 at 19:24









                          Anders Ellern Bilgrau

                          5,9331628




                          5,9331628






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53195214%2fhow-to-combine-frequency-tables-with-missing-values%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







                              這個網誌中的熱門文章

                              Academy of Television Arts & Sciences

                              L'Équipe

                              1995 France bombings