Apply same string status to all rows with similar application id and user id












0














For different combinations user_status_1 and user_status_2 and application_status=='complete' I have created an ultimate status, namely final_status. I want to apply the same final_status to all rows with same application_id and user_id. Please check beneath for a desired outcome.



My dataset



library(data.table)
library(dplyr)
df <- data.table(application_id = c(1,1,1,2,2,2,3,3,3),
user_id = c(123,123,123,456,456,456,789,789,789),
date = c("01/01/2018", "02/01/2018", "03/01/2018"),
application_status = c("incomplete", "details_verified", "complete"),
user_status_1 = c("x", "y", "z", "x", "y", "z", "x", "y", "z"),
user_status_2 = c("a","b", "c", "d", "e", "f", "g", "h", "i")) %>%
mutate(date = as.Date(date, "%d/%m/%Y"))


With outcome



 application_id user_id       date application_status user_status_1 user_status_2
1 123 2018-01-01 incomplete x a
1 123 2018-01-02 details_verified y b
1 123 2018-01-03 complete z c
2 456 2018-01-01 incomplete x d
2 456 2018-01-02 details_verified y e
2 456 2018-01-03 complete z f
3 789 2018-01-01 incomplete x g
3 789 2018-01-02 details_verified y h
3 789 2018-01-03 complete z i


My unsuccessful effort



df %>% group_by(application_id, user_id) %>%
mutate(final_status = case_when(any(
application_status == "complete" & user_status_1 == "z" & user_status_2 == "c" ~ "good",
application_status == "complete" & user_status_1 == "z" & user_status_2 == "f" ~ "great",
application_status == "complete" & user_status_1 == "z" & user_status_2 == "i" ~ "excellent"
)))


Desired outcome *(scroll horizontally to view all columns)*



application_id user_id       date application_status user_status_1 user_status_2 final_status
1 123 2018-01-01 incomplete x a good
1 123 2018-01-02 details_verified y b good
1 123 2018-01-03 complete z c good
2 456 2018-01-01 incomplete x d great
2 456 2018-01-02 details_verified y e great
2 456 2018-01-03 complete z f great
3 789 2018-01-01 incomplete x g excellent
3 789 2018-01-02 details_verified y h excellent
3 789 2018-01-03 complete z i excellent









share|improve this question



























    0














    For different combinations user_status_1 and user_status_2 and application_status=='complete' I have created an ultimate status, namely final_status. I want to apply the same final_status to all rows with same application_id and user_id. Please check beneath for a desired outcome.



    My dataset



    library(data.table)
    library(dplyr)
    df <- data.table(application_id = c(1,1,1,2,2,2,3,3,3),
    user_id = c(123,123,123,456,456,456,789,789,789),
    date = c("01/01/2018", "02/01/2018", "03/01/2018"),
    application_status = c("incomplete", "details_verified", "complete"),
    user_status_1 = c("x", "y", "z", "x", "y", "z", "x", "y", "z"),
    user_status_2 = c("a","b", "c", "d", "e", "f", "g", "h", "i")) %>%
    mutate(date = as.Date(date, "%d/%m/%Y"))


    With outcome



     application_id user_id       date application_status user_status_1 user_status_2
    1 123 2018-01-01 incomplete x a
    1 123 2018-01-02 details_verified y b
    1 123 2018-01-03 complete z c
    2 456 2018-01-01 incomplete x d
    2 456 2018-01-02 details_verified y e
    2 456 2018-01-03 complete z f
    3 789 2018-01-01 incomplete x g
    3 789 2018-01-02 details_verified y h
    3 789 2018-01-03 complete z i


    My unsuccessful effort



    df %>% group_by(application_id, user_id) %>%
    mutate(final_status = case_when(any(
    application_status == "complete" & user_status_1 == "z" & user_status_2 == "c" ~ "good",
    application_status == "complete" & user_status_1 == "z" & user_status_2 == "f" ~ "great",
    application_status == "complete" & user_status_1 == "z" & user_status_2 == "i" ~ "excellent"
    )))


    Desired outcome *(scroll horizontally to view all columns)*



    application_id user_id       date application_status user_status_1 user_status_2 final_status
    1 123 2018-01-01 incomplete x a good
    1 123 2018-01-02 details_verified y b good
    1 123 2018-01-03 complete z c good
    2 456 2018-01-01 incomplete x d great
    2 456 2018-01-02 details_verified y e great
    2 456 2018-01-03 complete z f great
    3 789 2018-01-01 incomplete x g excellent
    3 789 2018-01-02 details_verified y h excellent
    3 789 2018-01-03 complete z i excellent









    share|improve this question

























      0












      0








      0







      For different combinations user_status_1 and user_status_2 and application_status=='complete' I have created an ultimate status, namely final_status. I want to apply the same final_status to all rows with same application_id and user_id. Please check beneath for a desired outcome.



      My dataset



      library(data.table)
      library(dplyr)
      df <- data.table(application_id = c(1,1,1,2,2,2,3,3,3),
      user_id = c(123,123,123,456,456,456,789,789,789),
      date = c("01/01/2018", "02/01/2018", "03/01/2018"),
      application_status = c("incomplete", "details_verified", "complete"),
      user_status_1 = c("x", "y", "z", "x", "y", "z", "x", "y", "z"),
      user_status_2 = c("a","b", "c", "d", "e", "f", "g", "h", "i")) %>%
      mutate(date = as.Date(date, "%d/%m/%Y"))


      With outcome



       application_id user_id       date application_status user_status_1 user_status_2
      1 123 2018-01-01 incomplete x a
      1 123 2018-01-02 details_verified y b
      1 123 2018-01-03 complete z c
      2 456 2018-01-01 incomplete x d
      2 456 2018-01-02 details_verified y e
      2 456 2018-01-03 complete z f
      3 789 2018-01-01 incomplete x g
      3 789 2018-01-02 details_verified y h
      3 789 2018-01-03 complete z i


      My unsuccessful effort



      df %>% group_by(application_id, user_id) %>%
      mutate(final_status = case_when(any(
      application_status == "complete" & user_status_1 == "z" & user_status_2 == "c" ~ "good",
      application_status == "complete" & user_status_1 == "z" & user_status_2 == "f" ~ "great",
      application_status == "complete" & user_status_1 == "z" & user_status_2 == "i" ~ "excellent"
      )))


      Desired outcome *(scroll horizontally to view all columns)*



      application_id user_id       date application_status user_status_1 user_status_2 final_status
      1 123 2018-01-01 incomplete x a good
      1 123 2018-01-02 details_verified y b good
      1 123 2018-01-03 complete z c good
      2 456 2018-01-01 incomplete x d great
      2 456 2018-01-02 details_verified y e great
      2 456 2018-01-03 complete z f great
      3 789 2018-01-01 incomplete x g excellent
      3 789 2018-01-02 details_verified y h excellent
      3 789 2018-01-03 complete z i excellent









      share|improve this question













      For different combinations user_status_1 and user_status_2 and application_status=='complete' I have created an ultimate status, namely final_status. I want to apply the same final_status to all rows with same application_id and user_id. Please check beneath for a desired outcome.



      My dataset



      library(data.table)
      library(dplyr)
      df <- data.table(application_id = c(1,1,1,2,2,2,3,3,3),
      user_id = c(123,123,123,456,456,456,789,789,789),
      date = c("01/01/2018", "02/01/2018", "03/01/2018"),
      application_status = c("incomplete", "details_verified", "complete"),
      user_status_1 = c("x", "y", "z", "x", "y", "z", "x", "y", "z"),
      user_status_2 = c("a","b", "c", "d", "e", "f", "g", "h", "i")) %>%
      mutate(date = as.Date(date, "%d/%m/%Y"))


      With outcome



       application_id user_id       date application_status user_status_1 user_status_2
      1 123 2018-01-01 incomplete x a
      1 123 2018-01-02 details_verified y b
      1 123 2018-01-03 complete z c
      2 456 2018-01-01 incomplete x d
      2 456 2018-01-02 details_verified y e
      2 456 2018-01-03 complete z f
      3 789 2018-01-01 incomplete x g
      3 789 2018-01-02 details_verified y h
      3 789 2018-01-03 complete z i


      My unsuccessful effort



      df %>% group_by(application_id, user_id) %>%
      mutate(final_status = case_when(any(
      application_status == "complete" & user_status_1 == "z" & user_status_2 == "c" ~ "good",
      application_status == "complete" & user_status_1 == "z" & user_status_2 == "f" ~ "great",
      application_status == "complete" & user_status_1 == "z" & user_status_2 == "i" ~ "excellent"
      )))


      Desired outcome *(scroll horizontally to view all columns)*



      application_id user_id       date application_status user_status_1 user_status_2 final_status
      1 123 2018-01-01 incomplete x a good
      1 123 2018-01-02 details_verified y b good
      1 123 2018-01-03 complete z c good
      2 456 2018-01-01 incomplete x d great
      2 456 2018-01-02 details_verified y e great
      2 456 2018-01-03 complete z f great
      3 789 2018-01-01 incomplete x g excellent
      3 789 2018-01-02 details_verified y h excellent
      3 789 2018-01-03 complete z i excellent






      r dplyr data-manipulation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 20:22









      Greconomist

      130111




      130111
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You came close – you simply need to wrap each of the logical statements with any.



          df %>% 
          group_by(application_id, user_id) %>%
          mutate(final_status = case_when(
          any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "c") ~ "good",
          any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "f") ~ "great",
          any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "i") ~ "excellent"
          ))





          share|improve this answer





























            0














            Here is one option by creating a named vector first



            library(data.table)
            nm1 <- setNames(c('good', 'great', 'excellent'),
            c('completezc', 'completezf', 'completezi'))
            nm2 <- do.call(paste0, df[4:6])
            setDT(df)[, final_status := nm1[nm2]][,
            final_status := final_status[complete.cases(final_status)],
            .(application_id, user_id)]
            df
            # application_id user_id date application_status user_status_1 user_status_2 final_status
            #1: 1 123 2018-01-01 incomplete x a good
            #2: 1 123 2018-01-02 details_verified y b good
            #3: 1 123 2018-01-03 complete z c good
            #4: 2 456 2018-01-01 incomplete x d great
            #5: 2 456 2018-01-02 details_verified y e great
            #6: 2 456 2018-01-03 complete z f great
            #7: 3 789 2018-01-01 incomplete x g excellent
            #8: 3 789 2018-01-02 details_verified y h excellent
            #9: 3 789 2018-01-03 complete z i excellent




            Or do a join in tidyverse



            library(tidyverse)
            df %>%
            unite(newcol, !!! rlang::syms(names(.)[4:6]), sep="") %>%
            filter(str_detect(newcol, '^complete')) %>%
            transmute(application_id, user_id, final_status = nm1[newcol]) %>%
            right_join(df)





            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',
              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%2f53437610%2fapply-same-string-status-to-all-rows-with-similar-application-id-and-user-id%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









              1














              You came close – you simply need to wrap each of the logical statements with any.



              df %>% 
              group_by(application_id, user_id) %>%
              mutate(final_status = case_when(
              any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "c") ~ "good",
              any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "f") ~ "great",
              any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "i") ~ "excellent"
              ))





              share|improve this answer


























                1














                You came close – you simply need to wrap each of the logical statements with any.



                df %>% 
                group_by(application_id, user_id) %>%
                mutate(final_status = case_when(
                any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "c") ~ "good",
                any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "f") ~ "great",
                any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "i") ~ "excellent"
                ))





                share|improve this answer
























                  1












                  1








                  1






                  You came close – you simply need to wrap each of the logical statements with any.



                  df %>% 
                  group_by(application_id, user_id) %>%
                  mutate(final_status = case_when(
                  any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "c") ~ "good",
                  any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "f") ~ "great",
                  any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "i") ~ "excellent"
                  ))





                  share|improve this answer












                  You came close – you simply need to wrap each of the logical statements with any.



                  df %>% 
                  group_by(application_id, user_id) %>%
                  mutate(final_status = case_when(
                  any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "c") ~ "good",
                  any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "f") ~ "great",
                  any(application_status == "complete" & user_status_1 == "z" & user_status_2 == "i") ~ "excellent"
                  ))






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 at 20:48









                  Thomas K

                  2,178618




                  2,178618

























                      0














                      Here is one option by creating a named vector first



                      library(data.table)
                      nm1 <- setNames(c('good', 'great', 'excellent'),
                      c('completezc', 'completezf', 'completezi'))
                      nm2 <- do.call(paste0, df[4:6])
                      setDT(df)[, final_status := nm1[nm2]][,
                      final_status := final_status[complete.cases(final_status)],
                      .(application_id, user_id)]
                      df
                      # application_id user_id date application_status user_status_1 user_status_2 final_status
                      #1: 1 123 2018-01-01 incomplete x a good
                      #2: 1 123 2018-01-02 details_verified y b good
                      #3: 1 123 2018-01-03 complete z c good
                      #4: 2 456 2018-01-01 incomplete x d great
                      #5: 2 456 2018-01-02 details_verified y e great
                      #6: 2 456 2018-01-03 complete z f great
                      #7: 3 789 2018-01-01 incomplete x g excellent
                      #8: 3 789 2018-01-02 details_verified y h excellent
                      #9: 3 789 2018-01-03 complete z i excellent




                      Or do a join in tidyverse



                      library(tidyverse)
                      df %>%
                      unite(newcol, !!! rlang::syms(names(.)[4:6]), sep="") %>%
                      filter(str_detect(newcol, '^complete')) %>%
                      transmute(application_id, user_id, final_status = nm1[newcol]) %>%
                      right_join(df)





                      share|improve this answer




























                        0














                        Here is one option by creating a named vector first



                        library(data.table)
                        nm1 <- setNames(c('good', 'great', 'excellent'),
                        c('completezc', 'completezf', 'completezi'))
                        nm2 <- do.call(paste0, df[4:6])
                        setDT(df)[, final_status := nm1[nm2]][,
                        final_status := final_status[complete.cases(final_status)],
                        .(application_id, user_id)]
                        df
                        # application_id user_id date application_status user_status_1 user_status_2 final_status
                        #1: 1 123 2018-01-01 incomplete x a good
                        #2: 1 123 2018-01-02 details_verified y b good
                        #3: 1 123 2018-01-03 complete z c good
                        #4: 2 456 2018-01-01 incomplete x d great
                        #5: 2 456 2018-01-02 details_verified y e great
                        #6: 2 456 2018-01-03 complete z f great
                        #7: 3 789 2018-01-01 incomplete x g excellent
                        #8: 3 789 2018-01-02 details_verified y h excellent
                        #9: 3 789 2018-01-03 complete z i excellent




                        Or do a join in tidyverse



                        library(tidyverse)
                        df %>%
                        unite(newcol, !!! rlang::syms(names(.)[4:6]), sep="") %>%
                        filter(str_detect(newcol, '^complete')) %>%
                        transmute(application_id, user_id, final_status = nm1[newcol]) %>%
                        right_join(df)





                        share|improve this answer


























                          0












                          0








                          0






                          Here is one option by creating a named vector first



                          library(data.table)
                          nm1 <- setNames(c('good', 'great', 'excellent'),
                          c('completezc', 'completezf', 'completezi'))
                          nm2 <- do.call(paste0, df[4:6])
                          setDT(df)[, final_status := nm1[nm2]][,
                          final_status := final_status[complete.cases(final_status)],
                          .(application_id, user_id)]
                          df
                          # application_id user_id date application_status user_status_1 user_status_2 final_status
                          #1: 1 123 2018-01-01 incomplete x a good
                          #2: 1 123 2018-01-02 details_verified y b good
                          #3: 1 123 2018-01-03 complete z c good
                          #4: 2 456 2018-01-01 incomplete x d great
                          #5: 2 456 2018-01-02 details_verified y e great
                          #6: 2 456 2018-01-03 complete z f great
                          #7: 3 789 2018-01-01 incomplete x g excellent
                          #8: 3 789 2018-01-02 details_verified y h excellent
                          #9: 3 789 2018-01-03 complete z i excellent




                          Or do a join in tidyverse



                          library(tidyverse)
                          df %>%
                          unite(newcol, !!! rlang::syms(names(.)[4:6]), sep="") %>%
                          filter(str_detect(newcol, '^complete')) %>%
                          transmute(application_id, user_id, final_status = nm1[newcol]) %>%
                          right_join(df)





                          share|improve this answer














                          Here is one option by creating a named vector first



                          library(data.table)
                          nm1 <- setNames(c('good', 'great', 'excellent'),
                          c('completezc', 'completezf', 'completezi'))
                          nm2 <- do.call(paste0, df[4:6])
                          setDT(df)[, final_status := nm1[nm2]][,
                          final_status := final_status[complete.cases(final_status)],
                          .(application_id, user_id)]
                          df
                          # application_id user_id date application_status user_status_1 user_status_2 final_status
                          #1: 1 123 2018-01-01 incomplete x a good
                          #2: 1 123 2018-01-02 details_verified y b good
                          #3: 1 123 2018-01-03 complete z c good
                          #4: 2 456 2018-01-01 incomplete x d great
                          #5: 2 456 2018-01-02 details_verified y e great
                          #6: 2 456 2018-01-03 complete z f great
                          #7: 3 789 2018-01-01 incomplete x g excellent
                          #8: 3 789 2018-01-02 details_verified y h excellent
                          #9: 3 789 2018-01-03 complete z i excellent




                          Or do a join in tidyverse



                          library(tidyverse)
                          df %>%
                          unite(newcol, !!! rlang::syms(names(.)[4:6]), sep="") %>%
                          filter(str_detect(newcol, '^complete')) %>%
                          transmute(application_id, user_id, final_status = nm1[newcol]) %>%
                          right_join(df)






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 22 at 21:30

























                          answered Nov 22 at 21:06









                          akrun

                          396k13187260




                          396k13187260






























                              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%2f53437610%2fapply-same-string-status-to-all-rows-with-similar-application-id-and-user-id%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

                              A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                              Calculate evaluation metrics using cross_val_predict sklearn

                              Insert data from modal to MySQL (multiple modal on website)