Add a new column and tag based on others with ifelse












0














I have a data set, I want to a new column using ifelse, see below.



mydat<-data.frame(DB=c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL =c("NO","NO","YES","YES",'NO','NO','YES','YES'))

mydat$NEW <- ifelse(mydat$DB=="YES", "DB",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")))


But the output is not the one I want



DB  DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DB
YES YES DB


Expected output is instead



  DB  DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DBL
YES YES DBL









share|improve this question



























    0














    I have a data set, I want to a new column using ifelse, see below.



    mydat<-data.frame(DB=c("NO","NO","NO","NO",'YES','YES','YES','YES'),
    DL =c("NO","NO","YES","YES",'NO','NO','YES','YES'))

    mydat$NEW <- ifelse(mydat$DB=="YES", "DB",
    ifelse(mydat$DL=="YES", "DL",
    ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")))


    But the output is not the one I want



    DB  DL NEW
    NO NO NO
    NO NO NO
    NO YES DL
    NO YES DL
    YES NO DB
    YES NO DB
    YES YES DB
    YES YES DB


    Expected output is instead



      DB  DL NEW
    NO NO NO
    NO NO NO
    NO YES DL
    NO YES DL
    YES NO DB
    YES NO DB
    YES YES DBL
    YES YES DBL









    share|improve this question

























      0












      0








      0







      I have a data set, I want to a new column using ifelse, see below.



      mydat<-data.frame(DB=c("NO","NO","NO","NO",'YES','YES','YES','YES'),
      DL =c("NO","NO","YES","YES",'NO','NO','YES','YES'))

      mydat$NEW <- ifelse(mydat$DB=="YES", "DB",
      ifelse(mydat$DL=="YES", "DL",
      ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")))


      But the output is not the one I want



      DB  DL NEW
      NO NO NO
      NO NO NO
      NO YES DL
      NO YES DL
      YES NO DB
      YES NO DB
      YES YES DB
      YES YES DB


      Expected output is instead



        DB  DL NEW
      NO NO NO
      NO NO NO
      NO YES DL
      NO YES DL
      YES NO DB
      YES NO DB
      YES YES DBL
      YES YES DBL









      share|improve this question













      I have a data set, I want to a new column using ifelse, see below.



      mydat<-data.frame(DB=c("NO","NO","NO","NO",'YES','YES','YES','YES'),
      DL =c("NO","NO","YES","YES",'NO','NO','YES','YES'))

      mydat$NEW <- ifelse(mydat$DB=="YES", "DB",
      ifelse(mydat$DL=="YES", "DL",
      ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")))


      But the output is not the one I want



      DB  DL NEW
      NO NO NO
      NO NO NO
      NO YES DL
      NO YES DL
      YES NO DB
      YES NO DB
      YES YES DB
      YES YES DB


      Expected output is instead



        DB  DL NEW
      NO NO NO
      NO NO NO
      NO YES DL
      NO YES DL
      YES NO DB
      YES NO DB
      YES YES DBL
      YES YES DBL






      r if-statement






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 '18 at 13:03









      Al14Al14

      74431438




      74431438
























          3 Answers
          3






          active

          oldest

          votes


















          3














          Nested ifelse() statements get unwieldy pretty quickly, give dplyr::case_when() a go:



          data.frame(
          DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
          DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
          ) -> mydat

          dplyr::mutate(
          mydat, NEW = dplyr::case_when(
          ((DB == "YES") & (DL == "YES")) ~ "DBL",
          DB == "YES" ~ "DB",
          DL == "YES" ~ "DL",
          TRUE ~ "NO"
          )
          )
          ## DB DL NEW
          ## 1 NO NO NO
          ## 2 NO NO NO
          ## 3 NO YES DL
          ## 4 NO YES DL
          ## 5 YES NO DB
          ## 6 YES NO DB
          ## 7 YES YES DBL
          ## 8 YES YES DBL


          If you'd rather stick with base R, just reorder your conditions:



          with(
          mydat,
          ifelse(
          ((DB == "YES") & (DL == "YES")), "DBL",
          ifelse(
          (DB == "YES"), "DB",
          ifelse(
          (DL == "YES"), "DL", "NO"
          )
          )
          )
          ) -> mydat$NEW





          share|improve this answer































            2














            mydat$NEW <-    
            c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]

            # DB DL NEW
            #1 NO NO NO
            #2 NO NO NO
            #3 NO YES DL
            #4 NO YES DL
            #5 YES NO DB
            #6 YES NO DB
            #7 YES YES DBL
            #8 YES YES DBL





            share|improve this answer



















            • 2




              While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
              – hrbrmstr
              Nov 12 '18 at 13:38



















            1














            If you want know why it's not working, it's caused by the logical path.



            In the last condition



            ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")


            you only enter if



             mydat$DL=="YES"


            is FALSE, so this condition never applies.



            if you want to do it with nested if_elses try:



            mydat$NEW <- ifelse(mydat$DB=="YES"  & mydat$DL=="YES","DBL", 
            ifelse(mydat$DL=="YES", "DL",
            ifelse(mydat$DB=="YES","DB", "NO")))


            In addition, you could prefer dplyr if_else, cause is safer and a good practice.






            share|improve this answer





















            • Multiple nested dplyr::if_else() statements are just as bad as nested ifelse() statements. And, my answer covered everything in yours including the note to reorder the test conditions.
              – hrbrmstr
              Nov 12 '18 at 13:36













            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%2f53262779%2fadd-a-new-column-and-tag-based-on-others-with-ifelse%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            Nested ifelse() statements get unwieldy pretty quickly, give dplyr::case_when() a go:



            data.frame(
            DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
            DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
            ) -> mydat

            dplyr::mutate(
            mydat, NEW = dplyr::case_when(
            ((DB == "YES") & (DL == "YES")) ~ "DBL",
            DB == "YES" ~ "DB",
            DL == "YES" ~ "DL",
            TRUE ~ "NO"
            )
            )
            ## DB DL NEW
            ## 1 NO NO NO
            ## 2 NO NO NO
            ## 3 NO YES DL
            ## 4 NO YES DL
            ## 5 YES NO DB
            ## 6 YES NO DB
            ## 7 YES YES DBL
            ## 8 YES YES DBL


            If you'd rather stick with base R, just reorder your conditions:



            with(
            mydat,
            ifelse(
            ((DB == "YES") & (DL == "YES")), "DBL",
            ifelse(
            (DB == "YES"), "DB",
            ifelse(
            (DL == "YES"), "DL", "NO"
            )
            )
            )
            ) -> mydat$NEW





            share|improve this answer




























              3














              Nested ifelse() statements get unwieldy pretty quickly, give dplyr::case_when() a go:



              data.frame(
              DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
              DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
              ) -> mydat

              dplyr::mutate(
              mydat, NEW = dplyr::case_when(
              ((DB == "YES") & (DL == "YES")) ~ "DBL",
              DB == "YES" ~ "DB",
              DL == "YES" ~ "DL",
              TRUE ~ "NO"
              )
              )
              ## DB DL NEW
              ## 1 NO NO NO
              ## 2 NO NO NO
              ## 3 NO YES DL
              ## 4 NO YES DL
              ## 5 YES NO DB
              ## 6 YES NO DB
              ## 7 YES YES DBL
              ## 8 YES YES DBL


              If you'd rather stick with base R, just reorder your conditions:



              with(
              mydat,
              ifelse(
              ((DB == "YES") & (DL == "YES")), "DBL",
              ifelse(
              (DB == "YES"), "DB",
              ifelse(
              (DL == "YES"), "DL", "NO"
              )
              )
              )
              ) -> mydat$NEW





              share|improve this answer


























                3












                3








                3






                Nested ifelse() statements get unwieldy pretty quickly, give dplyr::case_when() a go:



                data.frame(
                DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
                DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
                ) -> mydat

                dplyr::mutate(
                mydat, NEW = dplyr::case_when(
                ((DB == "YES") & (DL == "YES")) ~ "DBL",
                DB == "YES" ~ "DB",
                DL == "YES" ~ "DL",
                TRUE ~ "NO"
                )
                )
                ## DB DL NEW
                ## 1 NO NO NO
                ## 2 NO NO NO
                ## 3 NO YES DL
                ## 4 NO YES DL
                ## 5 YES NO DB
                ## 6 YES NO DB
                ## 7 YES YES DBL
                ## 8 YES YES DBL


                If you'd rather stick with base R, just reorder your conditions:



                with(
                mydat,
                ifelse(
                ((DB == "YES") & (DL == "YES")), "DBL",
                ifelse(
                (DB == "YES"), "DB",
                ifelse(
                (DL == "YES"), "DL", "NO"
                )
                )
                )
                ) -> mydat$NEW





                share|improve this answer














                Nested ifelse() statements get unwieldy pretty quickly, give dplyr::case_when() a go:



                data.frame(
                DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
                DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
                ) -> mydat

                dplyr::mutate(
                mydat, NEW = dplyr::case_when(
                ((DB == "YES") & (DL == "YES")) ~ "DBL",
                DB == "YES" ~ "DB",
                DL == "YES" ~ "DL",
                TRUE ~ "NO"
                )
                )
                ## DB DL NEW
                ## 1 NO NO NO
                ## 2 NO NO NO
                ## 3 NO YES DL
                ## 4 NO YES DL
                ## 5 YES NO DB
                ## 6 YES NO DB
                ## 7 YES YES DBL
                ## 8 YES YES DBL


                If you'd rather stick with base R, just reorder your conditions:



                with(
                mydat,
                ifelse(
                ((DB == "YES") & (DL == "YES")), "DBL",
                ifelse(
                (DB == "YES"), "DB",
                ifelse(
                (DL == "YES"), "DL", "NO"
                )
                )
                )
                ) -> mydat$NEW






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 '18 at 13:37

























                answered Nov 12 '18 at 13:11









                hrbrmstrhrbrmstr

                60.3k687148




                60.3k687148

























                    2














                    mydat$NEW <-    
                    c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]

                    # DB DL NEW
                    #1 NO NO NO
                    #2 NO NO NO
                    #3 NO YES DL
                    #4 NO YES DL
                    #5 YES NO DB
                    #6 YES NO DB
                    #7 YES YES DBL
                    #8 YES YES DBL





                    share|improve this answer



















                    • 2




                      While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
                      – hrbrmstr
                      Nov 12 '18 at 13:38
















                    2














                    mydat$NEW <-    
                    c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]

                    # DB DL NEW
                    #1 NO NO NO
                    #2 NO NO NO
                    #3 NO YES DL
                    #4 NO YES DL
                    #5 YES NO DB
                    #6 YES NO DB
                    #7 YES YES DBL
                    #8 YES YES DBL





                    share|improve this answer



















                    • 2




                      While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
                      – hrbrmstr
                      Nov 12 '18 at 13:38














                    2












                    2








                    2






                    mydat$NEW <-    
                    c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]

                    # DB DL NEW
                    #1 NO NO NO
                    #2 NO NO NO
                    #3 NO YES DL
                    #4 NO YES DL
                    #5 YES NO DB
                    #6 YES NO DB
                    #7 YES YES DBL
                    #8 YES YES DBL





                    share|improve this answer














                    mydat$NEW <-    
                    c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]

                    # DB DL NEW
                    #1 NO NO NO
                    #2 NO NO NO
                    #3 NO YES DL
                    #4 NO YES DL
                    #5 YES NO DB
                    #6 YES NO DB
                    #7 YES YES DBL
                    #8 YES YES DBL






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 12 '18 at 13:38

























                    answered Nov 12 '18 at 13:36









                    Andre ElricoAndre Elrico

                    5,63311027




                    5,63311027








                    • 2




                      While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
                      – hrbrmstr
                      Nov 12 '18 at 13:38














                    • 2




                      While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
                      – hrbrmstr
                      Nov 12 '18 at 13:38








                    2




                    2




                    While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
                    – hrbrmstr
                    Nov 12 '18 at 13:38




                    While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
                    – hrbrmstr
                    Nov 12 '18 at 13:38











                    1














                    If you want know why it's not working, it's caused by the logical path.



                    In the last condition



                    ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")


                    you only enter if



                     mydat$DL=="YES"


                    is FALSE, so this condition never applies.



                    if you want to do it with nested if_elses try:



                    mydat$NEW <- ifelse(mydat$DB=="YES"  & mydat$DL=="YES","DBL", 
                    ifelse(mydat$DL=="YES", "DL",
                    ifelse(mydat$DB=="YES","DB", "NO")))


                    In addition, you could prefer dplyr if_else, cause is safer and a good practice.






                    share|improve this answer





















                    • Multiple nested dplyr::if_else() statements are just as bad as nested ifelse() statements. And, my answer covered everything in yours including the note to reorder the test conditions.
                      – hrbrmstr
                      Nov 12 '18 at 13:36


















                    1














                    If you want know why it's not working, it's caused by the logical path.



                    In the last condition



                    ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")


                    you only enter if



                     mydat$DL=="YES"


                    is FALSE, so this condition never applies.



                    if you want to do it with nested if_elses try:



                    mydat$NEW <- ifelse(mydat$DB=="YES"  & mydat$DL=="YES","DBL", 
                    ifelse(mydat$DL=="YES", "DL",
                    ifelse(mydat$DB=="YES","DB", "NO")))


                    In addition, you could prefer dplyr if_else, cause is safer and a good practice.






                    share|improve this answer





















                    • Multiple nested dplyr::if_else() statements are just as bad as nested ifelse() statements. And, my answer covered everything in yours including the note to reorder the test conditions.
                      – hrbrmstr
                      Nov 12 '18 at 13:36
















                    1












                    1








                    1






                    If you want know why it's not working, it's caused by the logical path.



                    In the last condition



                    ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")


                    you only enter if



                     mydat$DL=="YES"


                    is FALSE, so this condition never applies.



                    if you want to do it with nested if_elses try:



                    mydat$NEW <- ifelse(mydat$DB=="YES"  & mydat$DL=="YES","DBL", 
                    ifelse(mydat$DL=="YES", "DL",
                    ifelse(mydat$DB=="YES","DB", "NO")))


                    In addition, you could prefer dplyr if_else, cause is safer and a good practice.






                    share|improve this answer












                    If you want know why it's not working, it's caused by the logical path.



                    In the last condition



                    ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")


                    you only enter if



                     mydat$DL=="YES"


                    is FALSE, so this condition never applies.



                    if you want to do it with nested if_elses try:



                    mydat$NEW <- ifelse(mydat$DB=="YES"  & mydat$DL=="YES","DBL", 
                    ifelse(mydat$DL=="YES", "DL",
                    ifelse(mydat$DB=="YES","DB", "NO")))


                    In addition, you could prefer dplyr if_else, cause is safer and a good practice.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 '18 at 13:13









                    CarlosVecinaCarlosVecina

                    1915




                    1915












                    • Multiple nested dplyr::if_else() statements are just as bad as nested ifelse() statements. And, my answer covered everything in yours including the note to reorder the test conditions.
                      – hrbrmstr
                      Nov 12 '18 at 13:36




















                    • Multiple nested dplyr::if_else() statements are just as bad as nested ifelse() statements. And, my answer covered everything in yours including the note to reorder the test conditions.
                      – hrbrmstr
                      Nov 12 '18 at 13:36


















                    Multiple nested dplyr::if_else() statements are just as bad as nested ifelse() statements. And, my answer covered everything in yours including the note to reorder the test conditions.
                    – hrbrmstr
                    Nov 12 '18 at 13:36






                    Multiple nested dplyr::if_else() statements are just as bad as nested ifelse() statements. And, my answer covered everything in yours including the note to reorder the test conditions.
                    – hrbrmstr
                    Nov 12 '18 at 13:36




















                    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%2f53262779%2fadd-a-new-column-and-tag-based-on-others-with-ifelse%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

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation