Value matching with NA - missing values - using mutateRemove rows with all or some NAs (missing values) in data.framePerforming dplyr mutate on subset of columnsHow to evaluate stored column name in dplyr mutateUsing functions of multiple columns in a dplyr mutate_at callConditionally mutate columns based on column classdplyr mutate with null valueR dplyr::mutate pass all columns using “.” but limit rows by group_byWhen I don't know column names in data.frame, when I use dplyr mutate functioncan dplyr mutate() create a new tibble from an existing tibble?Mutate on nested column to results in unsupported class (data.frame)

Methodology: Writing unit tests for another developer

Are there examples of rowers who also fought?

Are there any individual aliens that have gained superpowers in the Marvel universe?

Can the pre-order traversal of two different trees be the same even though they are different?

Is "Busen" just the area between the breasts?

How do internally carried IR missiles acquire a lock?

Why does independence imply zero correlation?

Has a life raft ever been successfully deployed on a modern commercial flight?

Boss wants someone else to lead a project based on the idea I presented to him

How can a warlock learn from a spellbook?

What is the meaning of "понаехать"?

Helping ease my back pain by studying 13 hours everyday , even weekends

Is there a term for the belief that "if it's legal, it's moral"?

How can I prevent a user from copying files on another hard drive?

In the US, can a former president run again?

Am I legally required to provide a (GPL licensed) source code even after a project is abandoned?

Non-misogynistic way to say “asshole”?

What was the flower of Empress Taytu?

Is the continuity test limit resistance of a multimeter standard?

What is the most suitable position for a bishop here?

Cut the gold chain

How to work with PETG? Settings, caveats, etc

Is there any proof that high saturation and contrast makes a picture more appealing in social media?

Counterfeit checks were created for my account. How does this type of fraud work?



Value matching with NA - missing values - using mutate


Remove rows with all or some NAs (missing values) in data.framePerforming dplyr mutate on subset of columnsHow to evaluate stored column name in dplyr mutateUsing functions of multiple columns in a dplyr mutate_at callConditionally mutate columns based on column classdplyr mutate with null valueR dplyr::mutate pass all columns using “.” but limit rows by group_byWhen I don't know column names in data.frame, when I use dplyr mutate functioncan dplyr mutate() create a new tibble from an existing tibble?Mutate on nested column to results in unsupported class (data.frame)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








6

















I am somewhat stuck. Is there a better way than the below to do value matching considering NAs as "real values" within mutate?



library(dplyr)

data_foo <- data.frame(A= c(1:2, NA, 4, NA), B = c(1, 3, NA, NA, 4))


Not the desired output:



data_foo %>% mutate(irr = A==B)

#> A B irr
#> 1 1 1 TRUE
#> 2 2 3 FALSE
#> 3 NA NA NA
#> 4 4 NA NA
#> 5 NA 4 NA

data_foo %>% rowwise() %>% mutate(irr = A%in%B)

#> Source: local data frame [5 x 3]
#> Groups: <by row>
#>
#> # A tibble: 5 x 3
#> A B irr
#> <dbl> <dbl> <lgl>
#> 1 1 1 TRUE
#> 2 2 3 FALSE
#> 3 NA NA FALSE
#> 4 4 NA FALSE
#> 5 NA 4 FALSE


Desired output: The below shows the desired column, irr. I am using this somewhat cumbersome helper columns. Is there a shorter way?



data_foo %>% 
mutate(NA_A = is.na(A),
NA_B = is.na(B),
irr = if_else(is.na(A)|is.na(B), NA_A == NA_B, A == B))

#> A B NA_A NA_B irr
#> 1 1 1 FALSE FALSE TRUE
#> 2 2 3 FALSE FALSE FALSE
#> 3 NA NA TRUE TRUE TRUE
#> 4 4 NA FALSE TRUE FALSE
#> 5 NA 4 TRUE FALSE FALSE









share|improve this question




























    6

















    I am somewhat stuck. Is there a better way than the below to do value matching considering NAs as "real values" within mutate?



    library(dplyr)

    data_foo <- data.frame(A= c(1:2, NA, 4, NA), B = c(1, 3, NA, NA, 4))


    Not the desired output:



    data_foo %>% mutate(irr = A==B)

    #> A B irr
    #> 1 1 1 TRUE
    #> 2 2 3 FALSE
    #> 3 NA NA NA
    #> 4 4 NA NA
    #> 5 NA 4 NA

    data_foo %>% rowwise() %>% mutate(irr = A%in%B)

    #> Source: local data frame [5 x 3]
    #> Groups: <by row>
    #>
    #> # A tibble: 5 x 3
    #> A B irr
    #> <dbl> <dbl> <lgl>
    #> 1 1 1 TRUE
    #> 2 2 3 FALSE
    #> 3 NA NA FALSE
    #> 4 4 NA FALSE
    #> 5 NA 4 FALSE


    Desired output: The below shows the desired column, irr. I am using this somewhat cumbersome helper columns. Is there a shorter way?



    data_foo %>% 
    mutate(NA_A = is.na(A),
    NA_B = is.na(B),
    irr = if_else(is.na(A)|is.na(B), NA_A == NA_B, A == B))

    #> A B NA_A NA_B irr
    #> 1 1 1 FALSE FALSE TRUE
    #> 2 2 3 FALSE FALSE FALSE
    #> 3 NA NA TRUE TRUE TRUE
    #> 4 4 NA FALSE TRUE FALSE
    #> 5 NA 4 TRUE FALSE FALSE









    share|improve this question
























      6












      6








      6










      I am somewhat stuck. Is there a better way than the below to do value matching considering NAs as "real values" within mutate?



      library(dplyr)

      data_foo <- data.frame(A= c(1:2, NA, 4, NA), B = c(1, 3, NA, NA, 4))


      Not the desired output:



      data_foo %>% mutate(irr = A==B)

      #> A B irr
      #> 1 1 1 TRUE
      #> 2 2 3 FALSE
      #> 3 NA NA NA
      #> 4 4 NA NA
      #> 5 NA 4 NA

      data_foo %>% rowwise() %>% mutate(irr = A%in%B)

      #> Source: local data frame [5 x 3]
      #> Groups: <by row>
      #>
      #> # A tibble: 5 x 3
      #> A B irr
      #> <dbl> <dbl> <lgl>
      #> 1 1 1 TRUE
      #> 2 2 3 FALSE
      #> 3 NA NA FALSE
      #> 4 4 NA FALSE
      #> 5 NA 4 FALSE


      Desired output: The below shows the desired column, irr. I am using this somewhat cumbersome helper columns. Is there a shorter way?



      data_foo %>% 
      mutate(NA_A = is.na(A),
      NA_B = is.na(B),
      irr = if_else(is.na(A)|is.na(B), NA_A == NA_B, A == B))

      #> A B NA_A NA_B irr
      #> 1 1 1 FALSE FALSE TRUE
      #> 2 2 3 FALSE FALSE FALSE
      #> 3 NA NA TRUE TRUE TRUE
      #> 4 4 NA FALSE TRUE FALSE
      #> 5 NA 4 TRUE FALSE FALSE









      share|improve this question
















      I am somewhat stuck. Is there a better way than the below to do value matching considering NAs as "real values" within mutate?



      library(dplyr)

      data_foo <- data.frame(A= c(1:2, NA, 4, NA), B = c(1, 3, NA, NA, 4))


      Not the desired output:



      data_foo %>% mutate(irr = A==B)

      #> A B irr
      #> 1 1 1 TRUE
      #> 2 2 3 FALSE
      #> 3 NA NA NA
      #> 4 4 NA NA
      #> 5 NA 4 NA

      data_foo %>% rowwise() %>% mutate(irr = A%in%B)

      #> Source: local data frame [5 x 3]
      #> Groups: <by row>
      #>
      #> # A tibble: 5 x 3
      #> A B irr
      #> <dbl> <dbl> <lgl>
      #> 1 1 1 TRUE
      #> 2 2 3 FALSE
      #> 3 NA NA FALSE
      #> 4 4 NA FALSE
      #> 5 NA 4 FALSE


      Desired output: The below shows the desired column, irr. I am using this somewhat cumbersome helper columns. Is there a shorter way?



      data_foo %>% 
      mutate(NA_A = is.na(A),
      NA_B = is.na(B),
      irr = if_else(is.na(A)|is.na(B), NA_A == NA_B, A == B))

      #> A B NA_A NA_B irr
      #> 1 1 1 FALSE FALSE TRUE
      #> 2 2 3 FALSE FALSE FALSE
      #> 3 NA NA TRUE TRUE TRUE
      #> 4 4 NA FALSE TRUE FALSE
      #> 5 NA 4 TRUE FALSE FALSE






      r dplyr






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 8 hours ago









      TjeboTjebo

      2,7471635




      2,7471635






















          4 Answers
          4






          active

          oldest

          votes


















          2














          Maybe simpler than akrun's answer?

          Any of the two ways below will produce the expected result. Note that as.character won't do it, because the return value of as.character(NA) is NA_character_.



          data_foo %>%
          rowwise() %>%
          mutate(irr = paste(A) == paste(B))

          data_foo %>%
          rowwise() %>%
          mutate(irr = sQuote(A) == sQuote(B))

          #Source: local data frame [5 x 3]
          #Groups: <by row>
          #
          ## A tibble: 5 x 3
          # A B irr
          # <dbl> <dbl> <lgl>
          #1 1 1 TRUE
          #2 2 3 FALSE
          #3 NA NA TRUE
          #4 4 NA FALSE
          #5 NA 4 FALSE





          share|improve this answer


















          • 1





            Wouldn't data_foo %>% mutate(irr = paste(A) == paste(B)) works as well. Please update it in your code otherwise there are people who wants to post that as a solution even if there is a single character difference

            – akrun
            8 hours ago







          • 1





            Using paste, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!

            – Tjebo
            7 hours ago


















          4














          Using map2



          library(tidyverse)
          data_foo %>%
          mutate(irr = map2_lgl(A, B, `%in%`))
          # A B irr
          #1 1 1 TRUE
          #2 2 3 FALSE
          #3 NA NA TRUE
          #4 4 NA FALSE
          #5 NA 4 FALSE



          Or with setequal



          data_foo %>% 
          rowwise %>%
          mutate(irr = setequal(A, B))



          The above method is concise, but it is also loopy. We can replace the NA with a different value and then do the ==



          data_foo %>%
          mutate_all(list(new = ~ replace_na(., -999))) %>%
          transmute(A, B, irr = A_new == B_new)
          # A B irr
          #1 1 1 TRUE
          #2 2 3 FALSE
          #3 NA NA TRUE
          #4 4 NA FALSE
          #5 NA 4 FALSE



          Or with bind_cols and reduce



          data_foo %>%
          mutate_all(replace_na, -999) %>%
          reduce(`==`) %>%
          bind_cols(data_foo, irr = .)





          share|improve this answer




















          • 2





            wow, ok. That's totally above my level ... as often with your answers! Big fan of your concise code :) Thanks

            – Tjebo
            8 hours ago











          • @Tjebo It's okay. Rui's answer is better

            – akrun
            7 hours ago


















          2














          Could also be a possibility:



          data_foo %>%
          rowwise() %>%
          mutate(irr = identical(A, B)) %>%
          ungroup()

          A B irr
          <dbl> <dbl> <lgl>
          1 1 1 TRUE
          2 2 3 FALSE
          3 NA NA TRUE
          4 4 NA FALSE
          5 NA 4 FALSE





          share|improve this answer






























            1














            The coalesce function is useful if you want to perform an action when a value is NA



            data_foo %>% 
            mutate(irr = coalesce(A == B, is.na(A) & is.na(B)))

            # A B irr
            # 1 1 1 TRUE
            # 2 2 3 FALSE
            # 3 NA NA TRUE
            # 4 4 NA FALSE
            # 5 NA 4 FALSE


            Same thing for > 2 columns



            data_foo %>% 
            mutate(irr = coalesce(reduce(., `==`), rowMeans(is.na(.)) == 1))





            share|improve this answer























            • i deleted my post. Thanks for pointing out my mistake.

              – davsjob
              3 hours ago











            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%2f56635295%2fvalue-matching-with-na-missing-values-using-mutate%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            4 Answers
            4






            active

            oldest

            votes








            4 Answers
            4






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            Maybe simpler than akrun's answer?

            Any of the two ways below will produce the expected result. Note that as.character won't do it, because the return value of as.character(NA) is NA_character_.



            data_foo %>%
            rowwise() %>%
            mutate(irr = paste(A) == paste(B))

            data_foo %>%
            rowwise() %>%
            mutate(irr = sQuote(A) == sQuote(B))

            #Source: local data frame [5 x 3]
            #Groups: <by row>
            #
            ## A tibble: 5 x 3
            # A B irr
            # <dbl> <dbl> <lgl>
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE





            share|improve this answer


















            • 1





              Wouldn't data_foo %>% mutate(irr = paste(A) == paste(B)) works as well. Please update it in your code otherwise there are people who wants to post that as a solution even if there is a single character difference

              – akrun
              8 hours ago







            • 1





              Using paste, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!

              – Tjebo
              7 hours ago















            2














            Maybe simpler than akrun's answer?

            Any of the two ways below will produce the expected result. Note that as.character won't do it, because the return value of as.character(NA) is NA_character_.



            data_foo %>%
            rowwise() %>%
            mutate(irr = paste(A) == paste(B))

            data_foo %>%
            rowwise() %>%
            mutate(irr = sQuote(A) == sQuote(B))

            #Source: local data frame [5 x 3]
            #Groups: <by row>
            #
            ## A tibble: 5 x 3
            # A B irr
            # <dbl> <dbl> <lgl>
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE





            share|improve this answer


















            • 1





              Wouldn't data_foo %>% mutate(irr = paste(A) == paste(B)) works as well. Please update it in your code otherwise there are people who wants to post that as a solution even if there is a single character difference

              – akrun
              8 hours ago







            • 1





              Using paste, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!

              – Tjebo
              7 hours ago













            2












            2








            2







            Maybe simpler than akrun's answer?

            Any of the two ways below will produce the expected result. Note that as.character won't do it, because the return value of as.character(NA) is NA_character_.



            data_foo %>%
            rowwise() %>%
            mutate(irr = paste(A) == paste(B))

            data_foo %>%
            rowwise() %>%
            mutate(irr = sQuote(A) == sQuote(B))

            #Source: local data frame [5 x 3]
            #Groups: <by row>
            #
            ## A tibble: 5 x 3
            # A B irr
            # <dbl> <dbl> <lgl>
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE





            share|improve this answer













            Maybe simpler than akrun's answer?

            Any of the two ways below will produce the expected result. Note that as.character won't do it, because the return value of as.character(NA) is NA_character_.



            data_foo %>%
            rowwise() %>%
            mutate(irr = paste(A) == paste(B))

            data_foo %>%
            rowwise() %>%
            mutate(irr = sQuote(A) == sQuote(B))

            #Source: local data frame [5 x 3]
            #Groups: <by row>
            #
            ## A tibble: 5 x 3
            # A B irr
            # <dbl> <dbl> <lgl>
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 8 hours ago









            Rui BarradasRui Barradas

            20k61935




            20k61935







            • 1





              Wouldn't data_foo %>% mutate(irr = paste(A) == paste(B)) works as well. Please update it in your code otherwise there are people who wants to post that as a solution even if there is a single character difference

              – akrun
              8 hours ago







            • 1





              Using paste, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!

              – Tjebo
              7 hours ago












            • 1





              Wouldn't data_foo %>% mutate(irr = paste(A) == paste(B)) works as well. Please update it in your code otherwise there are people who wants to post that as a solution even if there is a single character difference

              – akrun
              8 hours ago







            • 1





              Using paste, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!

              – Tjebo
              7 hours ago







            1




            1





            Wouldn't data_foo %>% mutate(irr = paste(A) == paste(B)) works as well. Please update it in your code otherwise there are people who wants to post that as a solution even if there is a single character difference

            – akrun
            8 hours ago






            Wouldn't data_foo %>% mutate(irr = paste(A) == paste(B)) works as well. Please update it in your code otherwise there are people who wants to post that as a solution even if there is a single character difference

            – akrun
            8 hours ago





            1




            1





            Using paste, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!

            – Tjebo
            7 hours ago





            Using paste, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!

            – Tjebo
            7 hours ago













            4














            Using map2



            library(tidyverse)
            data_foo %>%
            mutate(irr = map2_lgl(A, B, `%in%`))
            # A B irr
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE



            Or with setequal



            data_foo %>% 
            rowwise %>%
            mutate(irr = setequal(A, B))



            The above method is concise, but it is also loopy. We can replace the NA with a different value and then do the ==



            data_foo %>%
            mutate_all(list(new = ~ replace_na(., -999))) %>%
            transmute(A, B, irr = A_new == B_new)
            # A B irr
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE



            Or with bind_cols and reduce



            data_foo %>%
            mutate_all(replace_na, -999) %>%
            reduce(`==`) %>%
            bind_cols(data_foo, irr = .)





            share|improve this answer




















            • 2





              wow, ok. That's totally above my level ... as often with your answers! Big fan of your concise code :) Thanks

              – Tjebo
              8 hours ago











            • @Tjebo It's okay. Rui's answer is better

              – akrun
              7 hours ago















            4














            Using map2



            library(tidyverse)
            data_foo %>%
            mutate(irr = map2_lgl(A, B, `%in%`))
            # A B irr
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE



            Or with setequal



            data_foo %>% 
            rowwise %>%
            mutate(irr = setequal(A, B))



            The above method is concise, but it is also loopy. We can replace the NA with a different value and then do the ==



            data_foo %>%
            mutate_all(list(new = ~ replace_na(., -999))) %>%
            transmute(A, B, irr = A_new == B_new)
            # A B irr
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE



            Or with bind_cols and reduce



            data_foo %>%
            mutate_all(replace_na, -999) %>%
            reduce(`==`) %>%
            bind_cols(data_foo, irr = .)





            share|improve this answer




















            • 2





              wow, ok. That's totally above my level ... as often with your answers! Big fan of your concise code :) Thanks

              – Tjebo
              8 hours ago











            • @Tjebo It's okay. Rui's answer is better

              – akrun
              7 hours ago













            4












            4








            4







            Using map2



            library(tidyverse)
            data_foo %>%
            mutate(irr = map2_lgl(A, B, `%in%`))
            # A B irr
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE



            Or with setequal



            data_foo %>% 
            rowwise %>%
            mutate(irr = setequal(A, B))



            The above method is concise, but it is also loopy. We can replace the NA with a different value and then do the ==



            data_foo %>%
            mutate_all(list(new = ~ replace_na(., -999))) %>%
            transmute(A, B, irr = A_new == B_new)
            # A B irr
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE



            Or with bind_cols and reduce



            data_foo %>%
            mutate_all(replace_na, -999) %>%
            reduce(`==`) %>%
            bind_cols(data_foo, irr = .)





            share|improve this answer















            Using map2



            library(tidyverse)
            data_foo %>%
            mutate(irr = map2_lgl(A, B, `%in%`))
            # A B irr
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE



            Or with setequal



            data_foo %>% 
            rowwise %>%
            mutate(irr = setequal(A, B))



            The above method is concise, but it is also loopy. We can replace the NA with a different value and then do the ==



            data_foo %>%
            mutate_all(list(new = ~ replace_na(., -999))) %>%
            transmute(A, B, irr = A_new == B_new)
            # A B irr
            #1 1 1 TRUE
            #2 2 3 FALSE
            #3 NA NA TRUE
            #4 4 NA FALSE
            #5 NA 4 FALSE



            Or with bind_cols and reduce



            data_foo %>%
            mutate_all(replace_na, -999) %>%
            reduce(`==`) %>%
            bind_cols(data_foo, irr = .)






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 8 hours ago

























            answered 8 hours ago









            akrunakrun

            441k14239324




            441k14239324







            • 2





              wow, ok. That's totally above my level ... as often with your answers! Big fan of your concise code :) Thanks

              – Tjebo
              8 hours ago











            • @Tjebo It's okay. Rui's answer is better

              – akrun
              7 hours ago












            • 2





              wow, ok. That's totally above my level ... as often with your answers! Big fan of your concise code :) Thanks

              – Tjebo
              8 hours ago











            • @Tjebo It's okay. Rui's answer is better

              – akrun
              7 hours ago







            2




            2





            wow, ok. That's totally above my level ... as often with your answers! Big fan of your concise code :) Thanks

            – Tjebo
            8 hours ago





            wow, ok. That's totally above my level ... as often with your answers! Big fan of your concise code :) Thanks

            – Tjebo
            8 hours ago













            @Tjebo It's okay. Rui's answer is better

            – akrun
            7 hours ago





            @Tjebo It's okay. Rui's answer is better

            – akrun
            7 hours ago











            2














            Could also be a possibility:



            data_foo %>%
            rowwise() %>%
            mutate(irr = identical(A, B)) %>%
            ungroup()

            A B irr
            <dbl> <dbl> <lgl>
            1 1 1 TRUE
            2 2 3 FALSE
            3 NA NA TRUE
            4 4 NA FALSE
            5 NA 4 FALSE





            share|improve this answer



























              2














              Could also be a possibility:



              data_foo %>%
              rowwise() %>%
              mutate(irr = identical(A, B)) %>%
              ungroup()

              A B irr
              <dbl> <dbl> <lgl>
              1 1 1 TRUE
              2 2 3 FALSE
              3 NA NA TRUE
              4 4 NA FALSE
              5 NA 4 FALSE





              share|improve this answer

























                2












                2








                2







                Could also be a possibility:



                data_foo %>%
                rowwise() %>%
                mutate(irr = identical(A, B)) %>%
                ungroup()

                A B irr
                <dbl> <dbl> <lgl>
                1 1 1 TRUE
                2 2 3 FALSE
                3 NA NA TRUE
                4 4 NA FALSE
                5 NA 4 FALSE





                share|improve this answer













                Could also be a possibility:



                data_foo %>%
                rowwise() %>%
                mutate(irr = identical(A, B)) %>%
                ungroup()

                A B irr
                <dbl> <dbl> <lgl>
                1 1 1 TRUE
                2 2 3 FALSE
                3 NA NA TRUE
                4 4 NA FALSE
                5 NA 4 FALSE






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 8 hours ago









                tmfmnktmfmnk

                7,2581821




                7,2581821





















                    1














                    The coalesce function is useful if you want to perform an action when a value is NA



                    data_foo %>% 
                    mutate(irr = coalesce(A == B, is.na(A) & is.na(B)))

                    # A B irr
                    # 1 1 1 TRUE
                    # 2 2 3 FALSE
                    # 3 NA NA TRUE
                    # 4 4 NA FALSE
                    # 5 NA 4 FALSE


                    Same thing for > 2 columns



                    data_foo %>% 
                    mutate(irr = coalesce(reduce(., `==`), rowMeans(is.na(.)) == 1))





                    share|improve this answer























                    • i deleted my post. Thanks for pointing out my mistake.

                      – davsjob
                      3 hours ago















                    1














                    The coalesce function is useful if you want to perform an action when a value is NA



                    data_foo %>% 
                    mutate(irr = coalesce(A == B, is.na(A) & is.na(B)))

                    # A B irr
                    # 1 1 1 TRUE
                    # 2 2 3 FALSE
                    # 3 NA NA TRUE
                    # 4 4 NA FALSE
                    # 5 NA 4 FALSE


                    Same thing for > 2 columns



                    data_foo %>% 
                    mutate(irr = coalesce(reduce(., `==`), rowMeans(is.na(.)) == 1))





                    share|improve this answer























                    • i deleted my post. Thanks for pointing out my mistake.

                      – davsjob
                      3 hours ago













                    1












                    1








                    1







                    The coalesce function is useful if you want to perform an action when a value is NA



                    data_foo %>% 
                    mutate(irr = coalesce(A == B, is.na(A) & is.na(B)))

                    # A B irr
                    # 1 1 1 TRUE
                    # 2 2 3 FALSE
                    # 3 NA NA TRUE
                    # 4 4 NA FALSE
                    # 5 NA 4 FALSE


                    Same thing for > 2 columns



                    data_foo %>% 
                    mutate(irr = coalesce(reduce(., `==`), rowMeans(is.na(.)) == 1))





                    share|improve this answer













                    The coalesce function is useful if you want to perform an action when a value is NA



                    data_foo %>% 
                    mutate(irr = coalesce(A == B, is.na(A) & is.na(B)))

                    # A B irr
                    # 1 1 1 TRUE
                    # 2 2 3 FALSE
                    # 3 NA NA TRUE
                    # 4 4 NA FALSE
                    # 5 NA 4 FALSE


                    Same thing for > 2 columns



                    data_foo %>% 
                    mutate(irr = coalesce(reduce(., `==`), rowMeans(is.na(.)) == 1))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 7 hours ago









                    IceCreamToucanIceCreamToucan

                    12.1k1819




                    12.1k1819












                    • i deleted my post. Thanks for pointing out my mistake.

                      – davsjob
                      3 hours ago

















                    • i deleted my post. Thanks for pointing out my mistake.

                      – davsjob
                      3 hours ago
















                    i deleted my post. Thanks for pointing out my mistake.

                    – davsjob
                    3 hours ago





                    i deleted my post. Thanks for pointing out my mistake.

                    – davsjob
                    3 hours ago

















                    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%2f56635295%2fvalue-matching-with-na-missing-values-using-mutate%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

                    Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                    Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                    Tom Holland Mục lục Đầu đời và giáo dục | Sự nghiệp | Cuộc sống cá nhân | Phim tham gia | Giải thưởng và đề cử | Chú thích | Liên kết ngoài | Trình đơn chuyển hướngProfile“Person Details for Thomas Stanley Holland, "England and Wales Birth Registration Index, 1837-2008" — FamilySearch.org”"Meet Tom Holland... the 16-year-old star of The Impossible""Schoolboy actor Tom Holland finds himself in Oscar contention for role in tsunami drama"“Naomi Watts on the Prince William and Harry's reaction to her film about the late Princess Diana”lưu trữ"Holland and Pflueger Are West End's Two New 'Billy Elliots'""I'm so envious of my son, the movie star! British writer Dominic Holland's spent 20 years trying to crack Hollywood - but he's been beaten to it by a very unlikely rival"“Richard and Margaret Povey of Jersey, Channel Islands, UK: Information about Thomas Stanley Holland”"Tom Holland to play Billy Elliot""New Billy Elliot leaving the garage"Billy Elliot the Musical - Tom Holland - Billy"A Tale of four Billys: Tom Holland""The Feel Good Factor""Thames Christian College schoolboys join Myleene Klass for The Feelgood Factor""Government launches £600,000 arts bursaries pilot""BILLY's Chapman, Holland, Gardner & Jackson-Keen Visit Prime Minister""Elton John 'blown away' by Billy Elliot fifth birthday" (video with John's interview and fragments of Holland's performance)"First News interviews Arrietty's Tom Holland"“33rd Critics' Circle Film Awards winners”“National Board of Review Current Awards”Bản gốc"Ron Howard Whaling Tale 'In The Heart Of The Sea' Casts Tom Holland"“'Spider-Man' Finds Tom Holland to Star as New Web-Slinger”lưu trữ“Captain America: Civil War (2016)”“Film Review: ‘Captain America: Civil War’”lưu trữ“‘Captain America: Civil War’ review: Choose your own avenger”lưu trữ“The Lost City of Z reviews”“Sony Pictures and Marvel Studios Find Their 'Spider-Man' Star and Director”“‘Mary Magdalene’, ‘Current War’ & ‘Wind River’ Get 2017 Release Dates From Weinstein”“Lionsgate Unleashing Daisy Ridley & Tom Holland Starrer ‘Chaos Walking’ In Cannes”“PTA's 'Master' Leads Chicago Film Critics Nominations, UPDATED: Houston and Indiana Critics Nominations”“Nominaciones Goya 2013 Telecinco Cinema – ENG”“Jameson Empire Film Awards: Martin Freeman wins best actor for performance in The Hobbit”“34th Annual Young Artist Awards”Bản gốc“Teen Choice Awards 2016—Captain America: Civil War Leads Second Wave of Nominations”“BAFTA Film Award Nominations: ‘La La Land’ Leads Race”“Saturn Awards Nominations 2017: 'Rogue One,' 'Walking Dead' Lead”Tom HollandTom HollandTom HollandTom Hollandmedia.gettyimages.comWorldCat Identities300279794no20130442900000 0004 0355 42791085670554170004732cb16706349t(data)XX5557367