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;
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
add a comment |
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
add a comment |
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
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
r dplyr
asked 8 hours ago
TjeboTjebo
2,7471635
2,7471635
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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
1
Wouldn'tdata_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
Usingpaste
, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!
– Tjebo
7 hours ago
add a comment |
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 = .)
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
add a comment |
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
add a comment |
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))
i deleted my post. Thanks for pointing out my mistake.
– davsjob
3 hours ago
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
1
Wouldn'tdata_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
Usingpaste
, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!
– Tjebo
7 hours ago
add a comment |
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
1
Wouldn'tdata_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
Usingpaste
, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!
– Tjebo
7 hours ago
add a comment |
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
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
answered 8 hours ago
Rui BarradasRui Barradas
20k61935
20k61935
1
Wouldn'tdata_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
Usingpaste
, and @akrun 's improvement, is a very neat and concise suggestion, and easy to understand as well :) Thanks both!
– Tjebo
7 hours ago
add a comment |
1
Wouldn'tdata_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
Usingpaste
, 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
add a comment |
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 = .)
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
add a comment |
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 = .)
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
add a comment |
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 = .)
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 = .)
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered 8 hours ago
tmfmnktmfmnk
7,2581821
7,2581821
add a comment |
add a comment |
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))
i deleted my post. Thanks for pointing out my mistake.
– davsjob
3 hours ago
add a comment |
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))
i deleted my post. Thanks for pointing out my mistake.
– davsjob
3 hours ago
add a comment |
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))
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))
answered 7 hours ago
IceCreamToucanIceCreamToucan
12.1k1819
12.1k1819
i deleted my post. Thanks for pointing out my mistake.
– davsjob
3 hours ago
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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