Add a new column and tag based on others with ifelse
I have a data set, I want to a new column using ifelse, see below.
mydat<-data.frame(DB=c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL =c("NO","NO","YES","YES",'NO','NO','YES','YES'))
mydat$NEW <- ifelse(mydat$DB=="YES", "DB",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")))
But the output is not the one I want
DB DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DB
YES YES DB
Expected output is instead
DB DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DBL
YES YES DBL
r if-statement
add a comment |
I have a data set, I want to a new column using ifelse, see below.
mydat<-data.frame(DB=c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL =c("NO","NO","YES","YES",'NO','NO','YES','YES'))
mydat$NEW <- ifelse(mydat$DB=="YES", "DB",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")))
But the output is not the one I want
DB DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DB
YES YES DB
Expected output is instead
DB DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DBL
YES YES DBL
r if-statement
add a comment |
I have a data set, I want to a new column using ifelse, see below.
mydat<-data.frame(DB=c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL =c("NO","NO","YES","YES",'NO','NO','YES','YES'))
mydat$NEW <- ifelse(mydat$DB=="YES", "DB",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")))
But the output is not the one I want
DB DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DB
YES YES DB
Expected output is instead
DB DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DBL
YES YES DBL
r if-statement
I have a data set, I want to a new column using ifelse, see below.
mydat<-data.frame(DB=c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL =c("NO","NO","YES","YES",'NO','NO','YES','YES'))
mydat$NEW <- ifelse(mydat$DB=="YES", "DB",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")))
But the output is not the one I want
DB DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DB
YES YES DB
Expected output is instead
DB DL NEW
NO NO NO
NO NO NO
NO YES DL
NO YES DL
YES NO DB
YES NO DB
YES YES DBL
YES YES DBL
r if-statement
r if-statement
asked Nov 12 '18 at 13:03
Al14Al14
74431438
74431438
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Nested ifelse()
statements get unwieldy pretty quickly, give dplyr::case_when()
a go:
data.frame(
DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
) -> mydat
dplyr::mutate(
mydat, NEW = dplyr::case_when(
((DB == "YES") & (DL == "YES")) ~ "DBL",
DB == "YES" ~ "DB",
DL == "YES" ~ "DL",
TRUE ~ "NO"
)
)
## DB DL NEW
## 1 NO NO NO
## 2 NO NO NO
## 3 NO YES DL
## 4 NO YES DL
## 5 YES NO DB
## 6 YES NO DB
## 7 YES YES DBL
## 8 YES YES DBL
If you'd rather stick with base R, just reorder your conditions:
with(
mydat,
ifelse(
((DB == "YES") & (DL == "YES")), "DBL",
ifelse(
(DB == "YES"), "DB",
ifelse(
(DL == "YES"), "DL", "NO"
)
)
)
) -> mydat$NEW
add a comment |
mydat$NEW <-
c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]
# DB DL NEW
#1 NO NO NO
#2 NO NO NO
#3 NO YES DL
#4 NO YES DL
#5 YES NO DB
#6 YES NO DB
#7 YES YES DBL
#8 YES YES DBL
2
While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
– hrbrmstr
Nov 12 '18 at 13:38
add a comment |
If you want know why it's not working, it's caused by the logical path.
In the last condition
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")
you only enter if
mydat$DL=="YES"
is FALSE, so this condition never applies.
if you want to do it with nested if_elses try:
mydat$NEW <- ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES","DB", "NO")))
In addition, you could prefer dplyr if_else, cause is safer and a good practice.
Multiple nesteddplyr::if_else()
statements are just as bad as nestedifelse()
statements. And, my answer covered everything in yours including the note to reorder the test conditions.
– hrbrmstr
Nov 12 '18 at 13:36
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%2f53262779%2fadd-a-new-column-and-tag-based-on-others-with-ifelse%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Nested ifelse()
statements get unwieldy pretty quickly, give dplyr::case_when()
a go:
data.frame(
DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
) -> mydat
dplyr::mutate(
mydat, NEW = dplyr::case_when(
((DB == "YES") & (DL == "YES")) ~ "DBL",
DB == "YES" ~ "DB",
DL == "YES" ~ "DL",
TRUE ~ "NO"
)
)
## DB DL NEW
## 1 NO NO NO
## 2 NO NO NO
## 3 NO YES DL
## 4 NO YES DL
## 5 YES NO DB
## 6 YES NO DB
## 7 YES YES DBL
## 8 YES YES DBL
If you'd rather stick with base R, just reorder your conditions:
with(
mydat,
ifelse(
((DB == "YES") & (DL == "YES")), "DBL",
ifelse(
(DB == "YES"), "DB",
ifelse(
(DL == "YES"), "DL", "NO"
)
)
)
) -> mydat$NEW
add a comment |
Nested ifelse()
statements get unwieldy pretty quickly, give dplyr::case_when()
a go:
data.frame(
DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
) -> mydat
dplyr::mutate(
mydat, NEW = dplyr::case_when(
((DB == "YES") & (DL == "YES")) ~ "DBL",
DB == "YES" ~ "DB",
DL == "YES" ~ "DL",
TRUE ~ "NO"
)
)
## DB DL NEW
## 1 NO NO NO
## 2 NO NO NO
## 3 NO YES DL
## 4 NO YES DL
## 5 YES NO DB
## 6 YES NO DB
## 7 YES YES DBL
## 8 YES YES DBL
If you'd rather stick with base R, just reorder your conditions:
with(
mydat,
ifelse(
((DB == "YES") & (DL == "YES")), "DBL",
ifelse(
(DB == "YES"), "DB",
ifelse(
(DL == "YES"), "DL", "NO"
)
)
)
) -> mydat$NEW
add a comment |
Nested ifelse()
statements get unwieldy pretty quickly, give dplyr::case_when()
a go:
data.frame(
DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
) -> mydat
dplyr::mutate(
mydat, NEW = dplyr::case_when(
((DB == "YES") & (DL == "YES")) ~ "DBL",
DB == "YES" ~ "DB",
DL == "YES" ~ "DL",
TRUE ~ "NO"
)
)
## DB DL NEW
## 1 NO NO NO
## 2 NO NO NO
## 3 NO YES DL
## 4 NO YES DL
## 5 YES NO DB
## 6 YES NO DB
## 7 YES YES DBL
## 8 YES YES DBL
If you'd rather stick with base R, just reorder your conditions:
with(
mydat,
ifelse(
((DB == "YES") & (DL == "YES")), "DBL",
ifelse(
(DB == "YES"), "DB",
ifelse(
(DL == "YES"), "DL", "NO"
)
)
)
) -> mydat$NEW
Nested ifelse()
statements get unwieldy pretty quickly, give dplyr::case_when()
a go:
data.frame(
DB = c("NO","NO","NO","NO",'YES','YES','YES','YES'),
DL = c("NO","NO","YES","YES",'NO','NO','YES','YES')
) -> mydat
dplyr::mutate(
mydat, NEW = dplyr::case_when(
((DB == "YES") & (DL == "YES")) ~ "DBL",
DB == "YES" ~ "DB",
DL == "YES" ~ "DL",
TRUE ~ "NO"
)
)
## DB DL NEW
## 1 NO NO NO
## 2 NO NO NO
## 3 NO YES DL
## 4 NO YES DL
## 5 YES NO DB
## 6 YES NO DB
## 7 YES YES DBL
## 8 YES YES DBL
If you'd rather stick with base R, just reorder your conditions:
with(
mydat,
ifelse(
((DB == "YES") & (DL == "YES")), "DBL",
ifelse(
(DB == "YES"), "DB",
ifelse(
(DL == "YES"), "DL", "NO"
)
)
)
) -> mydat$NEW
edited Nov 12 '18 at 13:37
answered Nov 12 '18 at 13:11
hrbrmstrhrbrmstr
60.3k687148
60.3k687148
add a comment |
add a comment |
mydat$NEW <-
c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]
# DB DL NEW
#1 NO NO NO
#2 NO NO NO
#3 NO YES DL
#4 NO YES DL
#5 YES NO DB
#6 YES NO DB
#7 YES YES DBL
#8 YES YES DBL
2
While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
– hrbrmstr
Nov 12 '18 at 13:38
add a comment |
mydat$NEW <-
c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]
# DB DL NEW
#1 NO NO NO
#2 NO NO NO
#3 NO YES DL
#4 NO YES DL
#5 YES NO DB
#6 YES NO DB
#7 YES YES DBL
#8 YES YES DBL
2
While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
– hrbrmstr
Nov 12 '18 at 13:38
add a comment |
mydat$NEW <-
c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]
# DB DL NEW
#1 NO NO NO
#2 NO NO NO
#3 NO YES DL
#4 NO YES DL
#5 YES NO DB
#6 YES NO DB
#7 YES YES DBL
#8 YES YES DBL
mydat$NEW <-
c("NO","DL","DB","DBL")[apply(mydat, 1 , function(x) sum(x == "YES") + (x[1] == "YES") + 1)]
# DB DL NEW
#1 NO NO NO
#2 NO NO NO
#3 NO YES DL
#4 NO YES DL
#5 YES NO DB
#6 YES NO DB
#7 YES YES DBL
#8 YES YES DBL
edited Nov 12 '18 at 13:38
answered Nov 12 '18 at 13:36
Andre ElricoAndre Elrico
5,63311027
5,63311027
2
While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
– hrbrmstr
Nov 12 '18 at 13:38
add a comment |
2
While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
– hrbrmstr
Nov 12 '18 at 13:38
2
2
While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
– hrbrmstr
Nov 12 '18 at 13:38
While this should likely never be used in production code, it is — nonetheless — a very clever hack :-)
– hrbrmstr
Nov 12 '18 at 13:38
add a comment |
If you want know why it's not working, it's caused by the logical path.
In the last condition
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")
you only enter if
mydat$DL=="YES"
is FALSE, so this condition never applies.
if you want to do it with nested if_elses try:
mydat$NEW <- ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES","DB", "NO")))
In addition, you could prefer dplyr if_else, cause is safer and a good practice.
Multiple nesteddplyr::if_else()
statements are just as bad as nestedifelse()
statements. And, my answer covered everything in yours including the note to reorder the test conditions.
– hrbrmstr
Nov 12 '18 at 13:36
add a comment |
If you want know why it's not working, it's caused by the logical path.
In the last condition
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")
you only enter if
mydat$DL=="YES"
is FALSE, so this condition never applies.
if you want to do it with nested if_elses try:
mydat$NEW <- ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES","DB", "NO")))
In addition, you could prefer dplyr if_else, cause is safer and a good practice.
Multiple nesteddplyr::if_else()
statements are just as bad as nestedifelse()
statements. And, my answer covered everything in yours including the note to reorder the test conditions.
– hrbrmstr
Nov 12 '18 at 13:36
add a comment |
If you want know why it's not working, it's caused by the logical path.
In the last condition
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")
you only enter if
mydat$DL=="YES"
is FALSE, so this condition never applies.
if you want to do it with nested if_elses try:
mydat$NEW <- ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES","DB", "NO")))
In addition, you could prefer dplyr if_else, cause is safer and a good practice.
If you want know why it's not working, it's caused by the logical path.
In the last condition
ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL", "NO")
you only enter if
mydat$DL=="YES"
is FALSE, so this condition never applies.
if you want to do it with nested if_elses try:
mydat$NEW <- ifelse(mydat$DB=="YES" & mydat$DL=="YES","DBL",
ifelse(mydat$DL=="YES", "DL",
ifelse(mydat$DB=="YES","DB", "NO")))
In addition, you could prefer dplyr if_else, cause is safer and a good practice.
answered Nov 12 '18 at 13:13
CarlosVecinaCarlosVecina
1915
1915
Multiple nesteddplyr::if_else()
statements are just as bad as nestedifelse()
statements. And, my answer covered everything in yours including the note to reorder the test conditions.
– hrbrmstr
Nov 12 '18 at 13:36
add a comment |
Multiple nesteddplyr::if_else()
statements are just as bad as nestedifelse()
statements. And, my answer covered everything in yours including the note to reorder the test conditions.
– hrbrmstr
Nov 12 '18 at 13:36
Multiple nested
dplyr::if_else()
statements are just as bad as nested ifelse()
statements. And, my answer covered everything in yours including the note to reorder the test conditions.– hrbrmstr
Nov 12 '18 at 13:36
Multiple nested
dplyr::if_else()
statements are just as bad as nested ifelse()
statements. And, my answer covered everything in yours including the note to reorder the test conditions.– hrbrmstr
Nov 12 '18 at 13:36
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%2f53262779%2fadd-a-new-column-and-tag-based-on-others-with-ifelse%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