Using case_when with multiple vectorsTest if a vector contains a given elementHow to sort a dataframe by multiple column(s)Group by multiple columns in dplyr, using string vector inputExtract a dplyr tbl column as a vectorcase_when in mutate pipeKeep value if not in case_when statementProgrammatically choosing which variables to put into dplyr pipeComplex ID Assignment with conditions, iterative calculations, and tolerance matchingpassing a vector into case_when inside mutate_atdplyr: case_when() over multiple columns with multiple conditions
Does the US Armed Forces refuse to recruit anyone with an IQ less than 83?
Is there an in-universe explanation of how Frodo's arrival in Valinor was recorded in the Red Book?
How are proofs normally constructed in a write up, in one line or split up into multiple lines?
What is the point of impeaching Trump?
Did the Soviet army intentionally send troops (e.g. penal battalions) running over minefields?
Avoiding dust scattering when you drill
Is there a way to replace Smite with Sharpness on a weapon?
Do jackscrews suffer from blowdown?
Is "Ram married his daughter" ambiguous?
Quote to show students don't have to fear making mistakes
Looking for circuit board material that can be dissolved
Sending mail to the Professor for PhD, after seeing his tweet
Would a horse be sufficient buffer to prevent injury when falling from a great height?
Realistically, how much do you need to start investing?
Why has Speaker Pelosi been so hesitant to impeach President Trump?
Does Bank Manager's discretion still exist in Mortgage Lending
Why do personal finance apps focus on outgoings rather than income
Why the first octet of a MAC address always end with a binary 0?
Could Boris Johnson face criminal charges for illegally proroguing Parliament?
As a team leader is it appropriate to bring in fundraiser candy?
GPLv3 forces us to make code available, but to who?
Notation clarity question for a conglomerate of accidentals
French license plates
IEEE 754 square root with Newton-Raphson
Using case_when with multiple vectors
Test if a vector contains a given elementHow to sort a dataframe by multiple column(s)Group by multiple columns in dplyr, using string vector inputExtract a dplyr tbl column as a vectorcase_when in mutate pipeKeep value if not in case_when statementProgrammatically choosing which variables to put into dplyr pipeComplex ID Assignment with conditions, iterative calculations, and tolerance matchingpassing a vector into case_when inside mutate_atdplyr: case_when() over multiple columns with multiple conditions
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
I am trying to use case_when
to modify/mutate a column based on two separate inputs. One that used to create the LHS logical and the respective input value on the RHS. An example is provided below.
library(dplyr)
library(purrr)
library(tibble)
df <- tibble(var = paste0(rep("var", 10), 1:10),
label = c("label1", "label2", rep(NA, 7), "label10"))
match_var <- paste0(rep("var", 7), 3:9)
new_labels <- paste0(rep("add_this_label", 7), 3:9)
df %>%
mutate(test = map2(match_var , new_labels,
~case_when(
var == .x ~ .y,
TRUE ~ label
)
))
I think the issue is that within case_when
everything is evaluated as expression but I'm not completely sure. One can manually type out all 7 lines within case_when
but my application requires me to accomplish this when the vectors match_vars
and new_labels
are very long - making manual typing of case_when
infeasible.
df %>%
mutate(label = case_when(
var == match_var[1] ~ new_labels[1],
var == match_var[2] ~ new_labels[2],
var == match_var[3] ~ new_labels[3],
var == match_var[4] ~ new_labels[4],
var == match_var[5] ~ new_labels[5],
var == match_var[6] ~ new_labels[6],
var == match_var[7] ~ new_labels[7],
TRUE ~ label
))
EDIT: the desired result can be accomplished using a for
loop but now I want to know if is this possible using case_when
and map2_*
function?
for (i in seq_along(match_var))
df$label <- ifelse(df$var == match_var[i], new_labels[i], df$label)
r dplyr
add a comment
|
I am trying to use case_when
to modify/mutate a column based on two separate inputs. One that used to create the LHS logical and the respective input value on the RHS. An example is provided below.
library(dplyr)
library(purrr)
library(tibble)
df <- tibble(var = paste0(rep("var", 10), 1:10),
label = c("label1", "label2", rep(NA, 7), "label10"))
match_var <- paste0(rep("var", 7), 3:9)
new_labels <- paste0(rep("add_this_label", 7), 3:9)
df %>%
mutate(test = map2(match_var , new_labels,
~case_when(
var == .x ~ .y,
TRUE ~ label
)
))
I think the issue is that within case_when
everything is evaluated as expression but I'm not completely sure. One can manually type out all 7 lines within case_when
but my application requires me to accomplish this when the vectors match_vars
and new_labels
are very long - making manual typing of case_when
infeasible.
df %>%
mutate(label = case_when(
var == match_var[1] ~ new_labels[1],
var == match_var[2] ~ new_labels[2],
var == match_var[3] ~ new_labels[3],
var == match_var[4] ~ new_labels[4],
var == match_var[5] ~ new_labels[5],
var == match_var[6] ~ new_labels[6],
var == match_var[7] ~ new_labels[7],
TRUE ~ label
))
EDIT: the desired result can be accomplished using a for
loop but now I want to know if is this possible using case_when
and map2_*
function?
for (i in seq_along(match_var))
df$label <- ifelse(df$var == match_var[i], new_labels[i], df$label)
r dplyr
add a comment
|
I am trying to use case_when
to modify/mutate a column based on two separate inputs. One that used to create the LHS logical and the respective input value on the RHS. An example is provided below.
library(dplyr)
library(purrr)
library(tibble)
df <- tibble(var = paste0(rep("var", 10), 1:10),
label = c("label1", "label2", rep(NA, 7), "label10"))
match_var <- paste0(rep("var", 7), 3:9)
new_labels <- paste0(rep("add_this_label", 7), 3:9)
df %>%
mutate(test = map2(match_var , new_labels,
~case_when(
var == .x ~ .y,
TRUE ~ label
)
))
I think the issue is that within case_when
everything is evaluated as expression but I'm not completely sure. One can manually type out all 7 lines within case_when
but my application requires me to accomplish this when the vectors match_vars
and new_labels
are very long - making manual typing of case_when
infeasible.
df %>%
mutate(label = case_when(
var == match_var[1] ~ new_labels[1],
var == match_var[2] ~ new_labels[2],
var == match_var[3] ~ new_labels[3],
var == match_var[4] ~ new_labels[4],
var == match_var[5] ~ new_labels[5],
var == match_var[6] ~ new_labels[6],
var == match_var[7] ~ new_labels[7],
TRUE ~ label
))
EDIT: the desired result can be accomplished using a for
loop but now I want to know if is this possible using case_when
and map2_*
function?
for (i in seq_along(match_var))
df$label <- ifelse(df$var == match_var[i], new_labels[i], df$label)
r dplyr
I am trying to use case_when
to modify/mutate a column based on two separate inputs. One that used to create the LHS logical and the respective input value on the RHS. An example is provided below.
library(dplyr)
library(purrr)
library(tibble)
df <- tibble(var = paste0(rep("var", 10), 1:10),
label = c("label1", "label2", rep(NA, 7), "label10"))
match_var <- paste0(rep("var", 7), 3:9)
new_labels <- paste0(rep("add_this_label", 7), 3:9)
df %>%
mutate(test = map2(match_var , new_labels,
~case_when(
var == .x ~ .y,
TRUE ~ label
)
))
I think the issue is that within case_when
everything is evaluated as expression but I'm not completely sure. One can manually type out all 7 lines within case_when
but my application requires me to accomplish this when the vectors match_vars
and new_labels
are very long - making manual typing of case_when
infeasible.
df %>%
mutate(label = case_when(
var == match_var[1] ~ new_labels[1],
var == match_var[2] ~ new_labels[2],
var == match_var[3] ~ new_labels[3],
var == match_var[4] ~ new_labels[4],
var == match_var[5] ~ new_labels[5],
var == match_var[6] ~ new_labels[6],
var == match_var[7] ~ new_labels[7],
TRUE ~ label
))
EDIT: the desired result can be accomplished using a for
loop but now I want to know if is this possible using case_when
and map2_*
function?
for (i in seq_along(match_var))
df$label <- ifelse(df$var == match_var[i], new_labels[i], df$label)
r dplyr
r dplyr
edited Mar 28 at 21:22
EJJ
asked Mar 28 at 21:17
EJJEJJ
1539 bronze badges
1539 bronze badges
add a comment
|
add a comment
|
2 Answers
2
active
oldest
votes
We create a named vector and use that to match the the values in 'var' so as to change the NA elements to 'new_labels'
library(tibble)
library(dplyr)
df %>%
mutate(label = case_when(is.na(label) ~
deframe(tibble(match_var, new_labels))[var],
TRUE ~ label))
# A tibble: 10 x 2
# var label
# <chr> <chr>
# 1 var1 label1
# 2 var2 label2
# 3 var3 add_this_label3
# 4 var4 add_this_label4
# 5 var5 add_this_label5
# 6 var6 add_this_label6
# 7 var7 add_this_label7
# 8 var8 add_this_label8
# 9 var9 add_this_label9
#10 var10 label10
NOTE: Instead of using deframe
, the named vector can be created with setNames
as well
this is great! if you don’t mind me asking, could you explain how named vectors work within ‘case_when’? Is it related to NSE?
– EJJ
Mar 28 at 21:56
add a comment
|
You can join the new labels to the data frame and fill in with the old label as necessary.
library("tidyverse")
df <- tibble(var = paste0(rep("var", 10), 1:10),
label = c("label1", "label2", rep(NA, 7), "label10"))
match_var <- paste0(rep("var", 7), 3:9)
new_label <- paste0(rep("add_this_label", 7), 3:9)
new_labels <- tibble(match_var, new_label)
df %>%
left_join(new_labels, by = c("var" = "match_var")) %>%
mutate(new_label = if_else(is.na(new_label), label, new_label))
#> # A tibble: 10 x 3
#> var label new_label
#> <chr> <chr> <chr>
#> 1 var1 label1 label1
#> 2 var2 label2 label2
#> 3 var3 <NA> add_this_label3
#> 4 var4 <NA> add_this_label4
#> 5 var5 <NA> add_this_label5
#> 6 var6 <NA> add_this_label6
#> 7 var7 <NA> add_this_label7
#> 8 var8 <NA> add_this_label8
#> 9 var9 <NA> add_this_label9
#> 10 var10 label10 label10
Created on 2019-03-28 by the reprex package (v0.2.1)
This is a creative approach using joins I didn’t consider. I might opt for @akrun solution just because I want to avoid joins due to performance but I will test that tomorrow
– EJJ
Mar 28 at 21:58
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/4.0/"u003ecc by-sa 4.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%2f55406974%2fusing-case-when-with-multiple-vectors%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
We create a named vector and use that to match the the values in 'var' so as to change the NA elements to 'new_labels'
library(tibble)
library(dplyr)
df %>%
mutate(label = case_when(is.na(label) ~
deframe(tibble(match_var, new_labels))[var],
TRUE ~ label))
# A tibble: 10 x 2
# var label
# <chr> <chr>
# 1 var1 label1
# 2 var2 label2
# 3 var3 add_this_label3
# 4 var4 add_this_label4
# 5 var5 add_this_label5
# 6 var6 add_this_label6
# 7 var7 add_this_label7
# 8 var8 add_this_label8
# 9 var9 add_this_label9
#10 var10 label10
NOTE: Instead of using deframe
, the named vector can be created with setNames
as well
this is great! if you don’t mind me asking, could you explain how named vectors work within ‘case_when’? Is it related to NSE?
– EJJ
Mar 28 at 21:56
add a comment
|
We create a named vector and use that to match the the values in 'var' so as to change the NA elements to 'new_labels'
library(tibble)
library(dplyr)
df %>%
mutate(label = case_when(is.na(label) ~
deframe(tibble(match_var, new_labels))[var],
TRUE ~ label))
# A tibble: 10 x 2
# var label
# <chr> <chr>
# 1 var1 label1
# 2 var2 label2
# 3 var3 add_this_label3
# 4 var4 add_this_label4
# 5 var5 add_this_label5
# 6 var6 add_this_label6
# 7 var7 add_this_label7
# 8 var8 add_this_label8
# 9 var9 add_this_label9
#10 var10 label10
NOTE: Instead of using deframe
, the named vector can be created with setNames
as well
this is great! if you don’t mind me asking, could you explain how named vectors work within ‘case_when’? Is it related to NSE?
– EJJ
Mar 28 at 21:56
add a comment
|
We create a named vector and use that to match the the values in 'var' so as to change the NA elements to 'new_labels'
library(tibble)
library(dplyr)
df %>%
mutate(label = case_when(is.na(label) ~
deframe(tibble(match_var, new_labels))[var],
TRUE ~ label))
# A tibble: 10 x 2
# var label
# <chr> <chr>
# 1 var1 label1
# 2 var2 label2
# 3 var3 add_this_label3
# 4 var4 add_this_label4
# 5 var5 add_this_label5
# 6 var6 add_this_label6
# 7 var7 add_this_label7
# 8 var8 add_this_label8
# 9 var9 add_this_label9
#10 var10 label10
NOTE: Instead of using deframe
, the named vector can be created with setNames
as well
We create a named vector and use that to match the the values in 'var' so as to change the NA elements to 'new_labels'
library(tibble)
library(dplyr)
df %>%
mutate(label = case_when(is.na(label) ~
deframe(tibble(match_var, new_labels))[var],
TRUE ~ label))
# A tibble: 10 x 2
# var label
# <chr> <chr>
# 1 var1 label1
# 2 var2 label2
# 3 var3 add_this_label3
# 4 var4 add_this_label4
# 5 var5 add_this_label5
# 6 var6 add_this_label6
# 7 var7 add_this_label7
# 8 var8 add_this_label8
# 9 var9 add_this_label9
#10 var10 label10
NOTE: Instead of using deframe
, the named vector can be created with setNames
as well
edited Mar 28 at 21:28
answered Mar 28 at 21:21
akrunakrun
470k15 gold badges261 silver badges343 bronze badges
470k15 gold badges261 silver badges343 bronze badges
this is great! if you don’t mind me asking, could you explain how named vectors work within ‘case_when’? Is it related to NSE?
– EJJ
Mar 28 at 21:56
add a comment
|
this is great! if you don’t mind me asking, could you explain how named vectors work within ‘case_when’? Is it related to NSE?
– EJJ
Mar 28 at 21:56
this is great! if you don’t mind me asking, could you explain how named vectors work within ‘case_when’? Is it related to NSE?
– EJJ
Mar 28 at 21:56
this is great! if you don’t mind me asking, could you explain how named vectors work within ‘case_when’? Is it related to NSE?
– EJJ
Mar 28 at 21:56
add a comment
|
You can join the new labels to the data frame and fill in with the old label as necessary.
library("tidyverse")
df <- tibble(var = paste0(rep("var", 10), 1:10),
label = c("label1", "label2", rep(NA, 7), "label10"))
match_var <- paste0(rep("var", 7), 3:9)
new_label <- paste0(rep("add_this_label", 7), 3:9)
new_labels <- tibble(match_var, new_label)
df %>%
left_join(new_labels, by = c("var" = "match_var")) %>%
mutate(new_label = if_else(is.na(new_label), label, new_label))
#> # A tibble: 10 x 3
#> var label new_label
#> <chr> <chr> <chr>
#> 1 var1 label1 label1
#> 2 var2 label2 label2
#> 3 var3 <NA> add_this_label3
#> 4 var4 <NA> add_this_label4
#> 5 var5 <NA> add_this_label5
#> 6 var6 <NA> add_this_label6
#> 7 var7 <NA> add_this_label7
#> 8 var8 <NA> add_this_label8
#> 9 var9 <NA> add_this_label9
#> 10 var10 label10 label10
Created on 2019-03-28 by the reprex package (v0.2.1)
This is a creative approach using joins I didn’t consider. I might opt for @akrun solution just because I want to avoid joins due to performance but I will test that tomorrow
– EJJ
Mar 28 at 21:58
add a comment
|
You can join the new labels to the data frame and fill in with the old label as necessary.
library("tidyverse")
df <- tibble(var = paste0(rep("var", 10), 1:10),
label = c("label1", "label2", rep(NA, 7), "label10"))
match_var <- paste0(rep("var", 7), 3:9)
new_label <- paste0(rep("add_this_label", 7), 3:9)
new_labels <- tibble(match_var, new_label)
df %>%
left_join(new_labels, by = c("var" = "match_var")) %>%
mutate(new_label = if_else(is.na(new_label), label, new_label))
#> # A tibble: 10 x 3
#> var label new_label
#> <chr> <chr> <chr>
#> 1 var1 label1 label1
#> 2 var2 label2 label2
#> 3 var3 <NA> add_this_label3
#> 4 var4 <NA> add_this_label4
#> 5 var5 <NA> add_this_label5
#> 6 var6 <NA> add_this_label6
#> 7 var7 <NA> add_this_label7
#> 8 var8 <NA> add_this_label8
#> 9 var9 <NA> add_this_label9
#> 10 var10 label10 label10
Created on 2019-03-28 by the reprex package (v0.2.1)
This is a creative approach using joins I didn’t consider. I might opt for @akrun solution just because I want to avoid joins due to performance but I will test that tomorrow
– EJJ
Mar 28 at 21:58
add a comment
|
You can join the new labels to the data frame and fill in with the old label as necessary.
library("tidyverse")
df <- tibble(var = paste0(rep("var", 10), 1:10),
label = c("label1", "label2", rep(NA, 7), "label10"))
match_var <- paste0(rep("var", 7), 3:9)
new_label <- paste0(rep("add_this_label", 7), 3:9)
new_labels <- tibble(match_var, new_label)
df %>%
left_join(new_labels, by = c("var" = "match_var")) %>%
mutate(new_label = if_else(is.na(new_label), label, new_label))
#> # A tibble: 10 x 3
#> var label new_label
#> <chr> <chr> <chr>
#> 1 var1 label1 label1
#> 2 var2 label2 label2
#> 3 var3 <NA> add_this_label3
#> 4 var4 <NA> add_this_label4
#> 5 var5 <NA> add_this_label5
#> 6 var6 <NA> add_this_label6
#> 7 var7 <NA> add_this_label7
#> 8 var8 <NA> add_this_label8
#> 9 var9 <NA> add_this_label9
#> 10 var10 label10 label10
Created on 2019-03-28 by the reprex package (v0.2.1)
You can join the new labels to the data frame and fill in with the old label as necessary.
library("tidyverse")
df <- tibble(var = paste0(rep("var", 10), 1:10),
label = c("label1", "label2", rep(NA, 7), "label10"))
match_var <- paste0(rep("var", 7), 3:9)
new_label <- paste0(rep("add_this_label", 7), 3:9)
new_labels <- tibble(match_var, new_label)
df %>%
left_join(new_labels, by = c("var" = "match_var")) %>%
mutate(new_label = if_else(is.na(new_label), label, new_label))
#> # A tibble: 10 x 3
#> var label new_label
#> <chr> <chr> <chr>
#> 1 var1 label1 label1
#> 2 var2 label2 label2
#> 3 var3 <NA> add_this_label3
#> 4 var4 <NA> add_this_label4
#> 5 var5 <NA> add_this_label5
#> 6 var6 <NA> add_this_label6
#> 7 var7 <NA> add_this_label7
#> 8 var8 <NA> add_this_label8
#> 9 var9 <NA> add_this_label9
#> 10 var10 label10 label10
Created on 2019-03-28 by the reprex package (v0.2.1)
answered Mar 28 at 21:31
dipetkovdipetkov
1,5061 silver badge8 bronze badges
1,5061 silver badge8 bronze badges
This is a creative approach using joins I didn’t consider. I might opt for @akrun solution just because I want to avoid joins due to performance but I will test that tomorrow
– EJJ
Mar 28 at 21:58
add a comment
|
This is a creative approach using joins I didn’t consider. I might opt for @akrun solution just because I want to avoid joins due to performance but I will test that tomorrow
– EJJ
Mar 28 at 21:58
This is a creative approach using joins I didn’t consider. I might opt for @akrun solution just because I want to avoid joins due to performance but I will test that tomorrow
– EJJ
Mar 28 at 21:58
This is a creative approach using joins I didn’t consider. I might opt for @akrun solution just because I want to avoid joins due to performance but I will test that tomorrow
– EJJ
Mar 28 at 21:58
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%2f55406974%2fusing-case-when-with-multiple-vectors%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