Create Multiple 2x2 Tables











up vote
1
down vote

favorite












I need to create multiple 2x2 tables across many variables.



sample data:



Month <- c("Jan", "Feb", "March", "April")
Quiz1 <- c("90", "80", "100", "70")
Quiz2 <- c("100", "70", "50", "20")
Quiz3 <- c("60", "35", "26", "80")

df <- data.frame(Month, Quiz1, Quiz2, Quiz3)


What I would like to do is get a 2x2 of each of each quiz by month and then save it in a csv so that I can create bar graphs - pie charts, etc in excel and they need to be distributed with non R users. It can be the same CSV or multiple ones, that does not matter. This is how I have been doing it (there are close to 20 quizes) but I know there has got to be a more efficient way.



Quiz1 <- table(df$Month, df$Quiz1)
Quiz2 <- table(df$Month, df$Quiz2)
Quiz3 <- table(df$Month, df$Quiz3)

write.csv(Quiz1, "filepath...")
write.csv(Quiz2, "filepath...")
write.csv(Quiz3, "filepath...")


I found this answer: loop for tabulation of variables in data frame and it would work great except it is just for frequency tables and I'm not sure how to adapt it for this scenario and I don't have enough points to leave a comment.



Thank you in advance for any help!










share|improve this question


























    up vote
    1
    down vote

    favorite












    I need to create multiple 2x2 tables across many variables.



    sample data:



    Month <- c("Jan", "Feb", "March", "April")
    Quiz1 <- c("90", "80", "100", "70")
    Quiz2 <- c("100", "70", "50", "20")
    Quiz3 <- c("60", "35", "26", "80")

    df <- data.frame(Month, Quiz1, Quiz2, Quiz3)


    What I would like to do is get a 2x2 of each of each quiz by month and then save it in a csv so that I can create bar graphs - pie charts, etc in excel and they need to be distributed with non R users. It can be the same CSV or multiple ones, that does not matter. This is how I have been doing it (there are close to 20 quizes) but I know there has got to be a more efficient way.



    Quiz1 <- table(df$Month, df$Quiz1)
    Quiz2 <- table(df$Month, df$Quiz2)
    Quiz3 <- table(df$Month, df$Quiz3)

    write.csv(Quiz1, "filepath...")
    write.csv(Quiz2, "filepath...")
    write.csv(Quiz3, "filepath...")


    I found this answer: loop for tabulation of variables in data frame and it would work great except it is just for frequency tables and I'm not sure how to adapt it for this scenario and I don't have enough points to leave a comment.



    Thank you in advance for any help!










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I need to create multiple 2x2 tables across many variables.



      sample data:



      Month <- c("Jan", "Feb", "March", "April")
      Quiz1 <- c("90", "80", "100", "70")
      Quiz2 <- c("100", "70", "50", "20")
      Quiz3 <- c("60", "35", "26", "80")

      df <- data.frame(Month, Quiz1, Quiz2, Quiz3)


      What I would like to do is get a 2x2 of each of each quiz by month and then save it in a csv so that I can create bar graphs - pie charts, etc in excel and they need to be distributed with non R users. It can be the same CSV or multiple ones, that does not matter. This is how I have been doing it (there are close to 20 quizes) but I know there has got to be a more efficient way.



      Quiz1 <- table(df$Month, df$Quiz1)
      Quiz2 <- table(df$Month, df$Quiz2)
      Quiz3 <- table(df$Month, df$Quiz3)

      write.csv(Quiz1, "filepath...")
      write.csv(Quiz2, "filepath...")
      write.csv(Quiz3, "filepath...")


      I found this answer: loop for tabulation of variables in data frame and it would work great except it is just for frequency tables and I'm not sure how to adapt it for this scenario and I don't have enough points to leave a comment.



      Thank you in advance for any help!










      share|improve this question













      I need to create multiple 2x2 tables across many variables.



      sample data:



      Month <- c("Jan", "Feb", "March", "April")
      Quiz1 <- c("90", "80", "100", "70")
      Quiz2 <- c("100", "70", "50", "20")
      Quiz3 <- c("60", "35", "26", "80")

      df <- data.frame(Month, Quiz1, Quiz2, Quiz3)


      What I would like to do is get a 2x2 of each of each quiz by month and then save it in a csv so that I can create bar graphs - pie charts, etc in excel and they need to be distributed with non R users. It can be the same CSV or multiple ones, that does not matter. This is how I have been doing it (there are close to 20 quizes) but I know there has got to be a more efficient way.



      Quiz1 <- table(df$Month, df$Quiz1)
      Quiz2 <- table(df$Month, df$Quiz2)
      Quiz3 <- table(df$Month, df$Quiz3)

      write.csv(Quiz1, "filepath...")
      write.csv(Quiz2, "filepath...")
      write.csv(Quiz3, "filepath...")


      I found this answer: loop for tabulation of variables in data frame and it would work great except it is just for frequency tables and I'm not sure how to adapt it for this scenario and I don't have enough points to leave a comment.



      Thank you in advance for any help!







      r






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 at 19:11









      user7777508

      5617




      5617
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          We can use lapply to loop through the columns except the 'Month' column, then get the table of the 'Month' with the looped column. The output will be a list of tables



          lst <- lapply(df[-1], function(x) table(df$Month, x))
          names(lst) <- paste0("Quiz", seq_along(lst))


          Instead of creating several global variables, a single list would serve the purpose. After the transformations, then use write.csv to write into different files



          lapply(names(lst), function(x) write.csv(lst[[x]], file = paste0(x, ".csv"))





          share|improve this answer























          • This works on the example data I have posted above but when I try to run this on the larger dataset with all of the quizes I am getting this error: "error in table(df$Month, x): all arguments must have the same length"
            – user7777508
            Nov 19 at 19:27










          • @user7777508 If you have a data.frame, it should work fine as the columns have equal length
            – akrun
            Nov 19 at 19:28






          • 1




            Great that worked perfectly! Thanks for your help.
            – user7777508
            Nov 19 at 19:54











          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%2f53381138%2fcreate-multiple-2x2-tables%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          We can use lapply to loop through the columns except the 'Month' column, then get the table of the 'Month' with the looped column. The output will be a list of tables



          lst <- lapply(df[-1], function(x) table(df$Month, x))
          names(lst) <- paste0("Quiz", seq_along(lst))


          Instead of creating several global variables, a single list would serve the purpose. After the transformations, then use write.csv to write into different files



          lapply(names(lst), function(x) write.csv(lst[[x]], file = paste0(x, ".csv"))





          share|improve this answer























          • This works on the example data I have posted above but when I try to run this on the larger dataset with all of the quizes I am getting this error: "error in table(df$Month, x): all arguments must have the same length"
            – user7777508
            Nov 19 at 19:27










          • @user7777508 If you have a data.frame, it should work fine as the columns have equal length
            – akrun
            Nov 19 at 19:28






          • 1




            Great that worked perfectly! Thanks for your help.
            – user7777508
            Nov 19 at 19:54















          up vote
          1
          down vote



          accepted










          We can use lapply to loop through the columns except the 'Month' column, then get the table of the 'Month' with the looped column. The output will be a list of tables



          lst <- lapply(df[-1], function(x) table(df$Month, x))
          names(lst) <- paste0("Quiz", seq_along(lst))


          Instead of creating several global variables, a single list would serve the purpose. After the transformations, then use write.csv to write into different files



          lapply(names(lst), function(x) write.csv(lst[[x]], file = paste0(x, ".csv"))





          share|improve this answer























          • This works on the example data I have posted above but when I try to run this on the larger dataset with all of the quizes I am getting this error: "error in table(df$Month, x): all arguments must have the same length"
            – user7777508
            Nov 19 at 19:27










          • @user7777508 If you have a data.frame, it should work fine as the columns have equal length
            – akrun
            Nov 19 at 19:28






          • 1




            Great that worked perfectly! Thanks for your help.
            – user7777508
            Nov 19 at 19:54













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          We can use lapply to loop through the columns except the 'Month' column, then get the table of the 'Month' with the looped column. The output will be a list of tables



          lst <- lapply(df[-1], function(x) table(df$Month, x))
          names(lst) <- paste0("Quiz", seq_along(lst))


          Instead of creating several global variables, a single list would serve the purpose. After the transformations, then use write.csv to write into different files



          lapply(names(lst), function(x) write.csv(lst[[x]], file = paste0(x, ".csv"))





          share|improve this answer














          We can use lapply to loop through the columns except the 'Month' column, then get the table of the 'Month' with the looped column. The output will be a list of tables



          lst <- lapply(df[-1], function(x) table(df$Month, x))
          names(lst) <- paste0("Quiz", seq_along(lst))


          Instead of creating several global variables, a single list would serve the purpose. After the transformations, then use write.csv to write into different files



          lapply(names(lst), function(x) write.csv(lst[[x]], file = paste0(x, ".csv"))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 at 19:21

























          answered Nov 19 at 19:12









          akrun

          391k13180253




          391k13180253












          • This works on the example data I have posted above but when I try to run this on the larger dataset with all of the quizes I am getting this error: "error in table(df$Month, x): all arguments must have the same length"
            – user7777508
            Nov 19 at 19:27










          • @user7777508 If you have a data.frame, it should work fine as the columns have equal length
            – akrun
            Nov 19 at 19:28






          • 1




            Great that worked perfectly! Thanks for your help.
            – user7777508
            Nov 19 at 19:54


















          • This works on the example data I have posted above but when I try to run this on the larger dataset with all of the quizes I am getting this error: "error in table(df$Month, x): all arguments must have the same length"
            – user7777508
            Nov 19 at 19:27










          • @user7777508 If you have a data.frame, it should work fine as the columns have equal length
            – akrun
            Nov 19 at 19:28






          • 1




            Great that worked perfectly! Thanks for your help.
            – user7777508
            Nov 19 at 19:54
















          This works on the example data I have posted above but when I try to run this on the larger dataset with all of the quizes I am getting this error: "error in table(df$Month, x): all arguments must have the same length"
          – user7777508
          Nov 19 at 19:27




          This works on the example data I have posted above but when I try to run this on the larger dataset with all of the quizes I am getting this error: "error in table(df$Month, x): all arguments must have the same length"
          – user7777508
          Nov 19 at 19:27












          @user7777508 If you have a data.frame, it should work fine as the columns have equal length
          – akrun
          Nov 19 at 19:28




          @user7777508 If you have a data.frame, it should work fine as the columns have equal length
          – akrun
          Nov 19 at 19:28




          1




          1




          Great that worked perfectly! Thanks for your help.
          – user7777508
          Nov 19 at 19:54




          Great that worked perfectly! Thanks for your help.
          – user7777508
          Nov 19 at 19:54


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53381138%2fcreate-multiple-2x2-tables%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python