Calculating row means across factor variables with strings for values












0














I have four variables that consist of values: Correct/Incorrect:



Var1      Var2      Var3      Var4 
Correct Correct Correct Correct
Incorrect Incorrect Incorrect Correct
Incorrect Incorrect Correct Incorrect
Incorrect Correct Correct Correct


I was wondering if there is an easy way to calculate average scores for each row that I can assign as a new variable. Without having to actually replace Correct/Incorrect with 1/0 in the dataset.



The score would be for correctness, so for example row1 would get a score of 1 and row4 would get a score of .75.










share|improve this question





























    0














    I have four variables that consist of values: Correct/Incorrect:



    Var1      Var2      Var3      Var4 
    Correct Correct Correct Correct
    Incorrect Incorrect Incorrect Correct
    Incorrect Incorrect Correct Incorrect
    Incorrect Correct Correct Correct


    I was wondering if there is an easy way to calculate average scores for each row that I can assign as a new variable. Without having to actually replace Correct/Incorrect with 1/0 in the dataset.



    The score would be for correctness, so for example row1 would get a score of 1 and row4 would get a score of .75.










    share|improve this question



























      0












      0








      0







      I have four variables that consist of values: Correct/Incorrect:



      Var1      Var2      Var3      Var4 
      Correct Correct Correct Correct
      Incorrect Incorrect Incorrect Correct
      Incorrect Incorrect Correct Incorrect
      Incorrect Correct Correct Correct


      I was wondering if there is an easy way to calculate average scores for each row that I can assign as a new variable. Without having to actually replace Correct/Incorrect with 1/0 in the dataset.



      The score would be for correctness, so for example row1 would get a score of 1 and row4 would get a score of .75.










      share|improve this question















      I have four variables that consist of values: Correct/Incorrect:



      Var1      Var2      Var3      Var4 
      Correct Correct Correct Correct
      Incorrect Incorrect Incorrect Correct
      Incorrect Incorrect Correct Incorrect
      Incorrect Correct Correct Correct


      I was wondering if there is an easy way to calculate average scores for each row that I can assign as a new variable. Without having to actually replace Correct/Incorrect with 1/0 in the dataset.



      The score would be for correctness, so for example row1 would get a score of 1 and row4 would get a score of .75.







      r average






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 at 20:50









      markus

      10.8k1029




      10.8k1029










      asked Nov 13 at 20:49









      Addison Maerz

      273




      273
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Use rowMeans



          df1$new_Var <- rowMeans(df1 == "Correct")
          df1
          # Var1 Var2 Var3 Var4 new_Var
          #1 Correct Correct Correct Correct 1.00
          #2 Incorrect Incorrect Incorrect Correct 0.25
          #3 Incorrect Incorrect Correct Incorrect 0.25
          #4 Incorrect Correct Correct Correct 0.75


          data



          df1 <- read.table(text = "Var1      Var2      Var3      Var4 
          Correct Correct Correct Correct
          Incorrect Incorrect Incorrect Correct
          Incorrect Incorrect Correct Incorrect
          Incorrect Correct Correct Correct", stringsAsFactors = FALSE, header = TRUE)





          share|improve this answer





















          • Thank you. Can you tell me what this would look like if I was extracting the 4 variables from a larger dataset? I am familiar with rowMeans but not how to integrate the logical expression.
            – Addison Maerz
            Nov 13 at 20:59










          • Sure. Can you update your question with more realistic example data? That is add two more columns and show what your real column names look like.
            – markus
            Nov 13 at 21:01












          • I believe I have figured it out. Here's what I did... Data df1 <- read.table(text = "Comp1 Comp2 Comp3 Comp4 Agree1 Agree2 Correct Correct Correct Correct 1 2 Incorrect Incorrect Incorrect Correct 4 3 Incorrect Incorrect Correct Incorrect 5 3 Incorrect Correct Correct Correct 1 1", stringsAsFactors = FALSE, header = TRUE) rowMeans(df1[ ,c("Comp1", "Comp2", "Comp3", "Comp4")] == "Correct") Thanks again for your help.
            – Addison Maerz
            Nov 13 at 21:06








          • 1




            Glad it worked. You might want to try rowMeans(df1[ ,paste0("Comp", 1:4)] == "Correct").
            – markus
            Nov 13 at 21:10













          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%2f53289280%2fcalculating-row-means-across-factor-variables-with-strings-for-values%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









          1














          Use rowMeans



          df1$new_Var <- rowMeans(df1 == "Correct")
          df1
          # Var1 Var2 Var3 Var4 new_Var
          #1 Correct Correct Correct Correct 1.00
          #2 Incorrect Incorrect Incorrect Correct 0.25
          #3 Incorrect Incorrect Correct Incorrect 0.25
          #4 Incorrect Correct Correct Correct 0.75


          data



          df1 <- read.table(text = "Var1      Var2      Var3      Var4 
          Correct Correct Correct Correct
          Incorrect Incorrect Incorrect Correct
          Incorrect Incorrect Correct Incorrect
          Incorrect Correct Correct Correct", stringsAsFactors = FALSE, header = TRUE)





          share|improve this answer





















          • Thank you. Can you tell me what this would look like if I was extracting the 4 variables from a larger dataset? I am familiar with rowMeans but not how to integrate the logical expression.
            – Addison Maerz
            Nov 13 at 20:59










          • Sure. Can you update your question with more realistic example data? That is add two more columns and show what your real column names look like.
            – markus
            Nov 13 at 21:01












          • I believe I have figured it out. Here's what I did... Data df1 <- read.table(text = "Comp1 Comp2 Comp3 Comp4 Agree1 Agree2 Correct Correct Correct Correct 1 2 Incorrect Incorrect Incorrect Correct 4 3 Incorrect Incorrect Correct Incorrect 5 3 Incorrect Correct Correct Correct 1 1", stringsAsFactors = FALSE, header = TRUE) rowMeans(df1[ ,c("Comp1", "Comp2", "Comp3", "Comp4")] == "Correct") Thanks again for your help.
            – Addison Maerz
            Nov 13 at 21:06








          • 1




            Glad it worked. You might want to try rowMeans(df1[ ,paste0("Comp", 1:4)] == "Correct").
            – markus
            Nov 13 at 21:10


















          1














          Use rowMeans



          df1$new_Var <- rowMeans(df1 == "Correct")
          df1
          # Var1 Var2 Var3 Var4 new_Var
          #1 Correct Correct Correct Correct 1.00
          #2 Incorrect Incorrect Incorrect Correct 0.25
          #3 Incorrect Incorrect Correct Incorrect 0.25
          #4 Incorrect Correct Correct Correct 0.75


          data



          df1 <- read.table(text = "Var1      Var2      Var3      Var4 
          Correct Correct Correct Correct
          Incorrect Incorrect Incorrect Correct
          Incorrect Incorrect Correct Incorrect
          Incorrect Correct Correct Correct", stringsAsFactors = FALSE, header = TRUE)





          share|improve this answer





















          • Thank you. Can you tell me what this would look like if I was extracting the 4 variables from a larger dataset? I am familiar with rowMeans but not how to integrate the logical expression.
            – Addison Maerz
            Nov 13 at 20:59










          • Sure. Can you update your question with more realistic example data? That is add two more columns and show what your real column names look like.
            – markus
            Nov 13 at 21:01












          • I believe I have figured it out. Here's what I did... Data df1 <- read.table(text = "Comp1 Comp2 Comp3 Comp4 Agree1 Agree2 Correct Correct Correct Correct 1 2 Incorrect Incorrect Incorrect Correct 4 3 Incorrect Incorrect Correct Incorrect 5 3 Incorrect Correct Correct Correct 1 1", stringsAsFactors = FALSE, header = TRUE) rowMeans(df1[ ,c("Comp1", "Comp2", "Comp3", "Comp4")] == "Correct") Thanks again for your help.
            – Addison Maerz
            Nov 13 at 21:06








          • 1




            Glad it worked. You might want to try rowMeans(df1[ ,paste0("Comp", 1:4)] == "Correct").
            – markus
            Nov 13 at 21:10
















          1












          1








          1






          Use rowMeans



          df1$new_Var <- rowMeans(df1 == "Correct")
          df1
          # Var1 Var2 Var3 Var4 new_Var
          #1 Correct Correct Correct Correct 1.00
          #2 Incorrect Incorrect Incorrect Correct 0.25
          #3 Incorrect Incorrect Correct Incorrect 0.25
          #4 Incorrect Correct Correct Correct 0.75


          data



          df1 <- read.table(text = "Var1      Var2      Var3      Var4 
          Correct Correct Correct Correct
          Incorrect Incorrect Incorrect Correct
          Incorrect Incorrect Correct Incorrect
          Incorrect Correct Correct Correct", stringsAsFactors = FALSE, header = TRUE)





          share|improve this answer












          Use rowMeans



          df1$new_Var <- rowMeans(df1 == "Correct")
          df1
          # Var1 Var2 Var3 Var4 new_Var
          #1 Correct Correct Correct Correct 1.00
          #2 Incorrect Incorrect Incorrect Correct 0.25
          #3 Incorrect Incorrect Correct Incorrect 0.25
          #4 Incorrect Correct Correct Correct 0.75


          data



          df1 <- read.table(text = "Var1      Var2      Var3      Var4 
          Correct Correct Correct Correct
          Incorrect Incorrect Incorrect Correct
          Incorrect Incorrect Correct Incorrect
          Incorrect Correct Correct Correct", stringsAsFactors = FALSE, header = TRUE)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 20:52









          markus

          10.8k1029




          10.8k1029












          • Thank you. Can you tell me what this would look like if I was extracting the 4 variables from a larger dataset? I am familiar with rowMeans but not how to integrate the logical expression.
            – Addison Maerz
            Nov 13 at 20:59










          • Sure. Can you update your question with more realistic example data? That is add two more columns and show what your real column names look like.
            – markus
            Nov 13 at 21:01












          • I believe I have figured it out. Here's what I did... Data df1 <- read.table(text = "Comp1 Comp2 Comp3 Comp4 Agree1 Agree2 Correct Correct Correct Correct 1 2 Incorrect Incorrect Incorrect Correct 4 3 Incorrect Incorrect Correct Incorrect 5 3 Incorrect Correct Correct Correct 1 1", stringsAsFactors = FALSE, header = TRUE) rowMeans(df1[ ,c("Comp1", "Comp2", "Comp3", "Comp4")] == "Correct") Thanks again for your help.
            – Addison Maerz
            Nov 13 at 21:06








          • 1




            Glad it worked. You might want to try rowMeans(df1[ ,paste0("Comp", 1:4)] == "Correct").
            – markus
            Nov 13 at 21:10




















          • Thank you. Can you tell me what this would look like if I was extracting the 4 variables from a larger dataset? I am familiar with rowMeans but not how to integrate the logical expression.
            – Addison Maerz
            Nov 13 at 20:59










          • Sure. Can you update your question with more realistic example data? That is add two more columns and show what your real column names look like.
            – markus
            Nov 13 at 21:01












          • I believe I have figured it out. Here's what I did... Data df1 <- read.table(text = "Comp1 Comp2 Comp3 Comp4 Agree1 Agree2 Correct Correct Correct Correct 1 2 Incorrect Incorrect Incorrect Correct 4 3 Incorrect Incorrect Correct Incorrect 5 3 Incorrect Correct Correct Correct 1 1", stringsAsFactors = FALSE, header = TRUE) rowMeans(df1[ ,c("Comp1", "Comp2", "Comp3", "Comp4")] == "Correct") Thanks again for your help.
            – Addison Maerz
            Nov 13 at 21:06








          • 1




            Glad it worked. You might want to try rowMeans(df1[ ,paste0("Comp", 1:4)] == "Correct").
            – markus
            Nov 13 at 21:10


















          Thank you. Can you tell me what this would look like if I was extracting the 4 variables from a larger dataset? I am familiar with rowMeans but not how to integrate the logical expression.
          – Addison Maerz
          Nov 13 at 20:59




          Thank you. Can you tell me what this would look like if I was extracting the 4 variables from a larger dataset? I am familiar with rowMeans but not how to integrate the logical expression.
          – Addison Maerz
          Nov 13 at 20:59












          Sure. Can you update your question with more realistic example data? That is add two more columns and show what your real column names look like.
          – markus
          Nov 13 at 21:01






          Sure. Can you update your question with more realistic example data? That is add two more columns and show what your real column names look like.
          – markus
          Nov 13 at 21:01














          I believe I have figured it out. Here's what I did... Data df1 <- read.table(text = "Comp1 Comp2 Comp3 Comp4 Agree1 Agree2 Correct Correct Correct Correct 1 2 Incorrect Incorrect Incorrect Correct 4 3 Incorrect Incorrect Correct Incorrect 5 3 Incorrect Correct Correct Correct 1 1", stringsAsFactors = FALSE, header = TRUE) rowMeans(df1[ ,c("Comp1", "Comp2", "Comp3", "Comp4")] == "Correct") Thanks again for your help.
          – Addison Maerz
          Nov 13 at 21:06






          I believe I have figured it out. Here's what I did... Data df1 <- read.table(text = "Comp1 Comp2 Comp3 Comp4 Agree1 Agree2 Correct Correct Correct Correct 1 2 Incorrect Incorrect Incorrect Correct 4 3 Incorrect Incorrect Correct Incorrect 5 3 Incorrect Correct Correct Correct 1 1", stringsAsFactors = FALSE, header = TRUE) rowMeans(df1[ ,c("Comp1", "Comp2", "Comp3", "Comp4")] == "Correct") Thanks again for your help.
          – Addison Maerz
          Nov 13 at 21:06






          1




          1




          Glad it worked. You might want to try rowMeans(df1[ ,paste0("Comp", 1:4)] == "Correct").
          – markus
          Nov 13 at 21:10






          Glad it worked. You might want to try rowMeans(df1[ ,paste0("Comp", 1:4)] == "Correct").
          – markus
          Nov 13 at 21:10




















          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%2f53289280%2fcalculating-row-means-across-factor-variables-with-strings-for-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







          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word