Create a new column based in If Statments R
I would like to know if I can create if statements based in characters. such is
this is not a real code I am trying to understand if this is possible
if {taskStaffName != 'Carl Goss' add 80
else if taskStaffName = 'Carl Goss' add 40
else if taskStaffName = 'Ama Fh' add 44
Data Sample:
taskStaffName false true
1 Ama Fh 11 0
2 Bar Mi 14 88
3 Briny Rey 6 0
4 Ben Le 20 65
5 Co Wams 165 398
6 Don Chrensen 7 106
7 Da Cari 0 5
8 Geth by 9 546
9 Hen Vaeghem 34 157
10 Jemy Haran 2 35
11 Joph Le 9 22
12 Carl Goss 69 360
Outcome Desired:
taskStaffName false true Budget
1 Ama Fh 11 0 44
2 Bar Mi 14 88 80
3 Briny Rey 6 0 80
4 Ben Le 20 65 80
5 Co Wams 165 398 80
6 Don Chrensen 7 106 80
7 Da Cari 0 5 80
8 Geth by 9 546 80
9 Hen Vaeghem 34 157 80
10 Jemy Haran 2 35 80
11 Joph Le 9 22 80
12 Carl Goss 69 360 40
r function if-statement
add a comment |
I would like to know if I can create if statements based in characters. such is
this is not a real code I am trying to understand if this is possible
if {taskStaffName != 'Carl Goss' add 80
else if taskStaffName = 'Carl Goss' add 40
else if taskStaffName = 'Ama Fh' add 44
Data Sample:
taskStaffName false true
1 Ama Fh 11 0
2 Bar Mi 14 88
3 Briny Rey 6 0
4 Ben Le 20 65
5 Co Wams 165 398
6 Don Chrensen 7 106
7 Da Cari 0 5
8 Geth by 9 546
9 Hen Vaeghem 34 157
10 Jemy Haran 2 35
11 Joph Le 9 22
12 Carl Goss 69 360
Outcome Desired:
taskStaffName false true Budget
1 Ama Fh 11 0 44
2 Bar Mi 14 88 80
3 Briny Rey 6 0 80
4 Ben Le 20 65 80
5 Co Wams 165 398 80
6 Don Chrensen 7 106 80
7 Da Cari 0 5 80
8 Geth by 9 546 80
9 Hen Vaeghem 34 157 80
10 Jemy Haran 2 35 80
11 Joph Le 9 22 80
12 Carl Goss 69 360 40
r function if-statement
add a comment |
I would like to know if I can create if statements based in characters. such is
this is not a real code I am trying to understand if this is possible
if {taskStaffName != 'Carl Goss' add 80
else if taskStaffName = 'Carl Goss' add 40
else if taskStaffName = 'Ama Fh' add 44
Data Sample:
taskStaffName false true
1 Ama Fh 11 0
2 Bar Mi 14 88
3 Briny Rey 6 0
4 Ben Le 20 65
5 Co Wams 165 398
6 Don Chrensen 7 106
7 Da Cari 0 5
8 Geth by 9 546
9 Hen Vaeghem 34 157
10 Jemy Haran 2 35
11 Joph Le 9 22
12 Carl Goss 69 360
Outcome Desired:
taskStaffName false true Budget
1 Ama Fh 11 0 44
2 Bar Mi 14 88 80
3 Briny Rey 6 0 80
4 Ben Le 20 65 80
5 Co Wams 165 398 80
6 Don Chrensen 7 106 80
7 Da Cari 0 5 80
8 Geth by 9 546 80
9 Hen Vaeghem 34 157 80
10 Jemy Haran 2 35 80
11 Joph Le 9 22 80
12 Carl Goss 69 360 40
r function if-statement
I would like to know if I can create if statements based in characters. such is
this is not a real code I am trying to understand if this is possible
if {taskStaffName != 'Carl Goss' add 80
else if taskStaffName = 'Carl Goss' add 40
else if taskStaffName = 'Ama Fh' add 44
Data Sample:
taskStaffName false true
1 Ama Fh 11 0
2 Bar Mi 14 88
3 Briny Rey 6 0
4 Ben Le 20 65
5 Co Wams 165 398
6 Don Chrensen 7 106
7 Da Cari 0 5
8 Geth by 9 546
9 Hen Vaeghem 34 157
10 Jemy Haran 2 35
11 Joph Le 9 22
12 Carl Goss 69 360
Outcome Desired:
taskStaffName false true Budget
1 Ama Fh 11 0 44
2 Bar Mi 14 88 80
3 Briny Rey 6 0 80
4 Ben Le 20 65 80
5 Co Wams 165 398 80
6 Don Chrensen 7 106 80
7 Da Cari 0 5 80
8 Geth by 9 546 80
9 Hen Vaeghem 34 157 80
10 Jemy Haran 2 35 80
11 Joph Le 9 22 80
12 Carl Goss 69 360 40
r function if-statement
r function if-statement
edited Nov 13 '18 at 6:40
luis vergara
asked Nov 13 '18 at 6:34
luis vergaraluis vergara
266
266
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Yes, we can try using ifelse here:
df$Budget <- ifelse(df$taskStaffName == "Carl Goss", 40, 80)
Assuming you actually wanted to add a value to the budget column, then use:
df$Budget <- df$Budget + ifelse(df$taskStaffName == "Carl Goss", 40, 80)
To handle more than two uses cases, we can try using case_when from the dplyr package:
df$Budget <- case_when(
df$taskStaffName == "Carl Goss" ~ 40,
df$taskStaffName == "Ama Fh" ~ 44,
TRUE ~ 80
)
The way of doing this in base R would be to just nest calls to ifelse, but this quickly becomes unreadable.
What would you do if they are 2 that are differents Ama Fh 11 0 44, Carl Goss 69 360 40 and the rest 80, sorry I just edited my post. Thanks by the way
– luis vergara
Nov 13 '18 at 6:41
Error: unexpected '=' in "Exp$Budget <- Exp$Budget + ifelse(Exp$taskStaffName ="
– luis vergara
Nov 13 '18 at 6:46
Sorry, I keep doing this. You need to use==as the equality operator. Keep in mind that much of the code posted to Stack Overflow hasn't (and sometimes can't be) tested.
– Tim Biegeleisen
Nov 13 '18 at 6:47
Great is working! Thanks!
– luis vergara
Nov 13 '18 at 6:53
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%2f53275106%2fcreate-a-new-column-based-in-if-statments-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, we can try using ifelse here:
df$Budget <- ifelse(df$taskStaffName == "Carl Goss", 40, 80)
Assuming you actually wanted to add a value to the budget column, then use:
df$Budget <- df$Budget + ifelse(df$taskStaffName == "Carl Goss", 40, 80)
To handle more than two uses cases, we can try using case_when from the dplyr package:
df$Budget <- case_when(
df$taskStaffName == "Carl Goss" ~ 40,
df$taskStaffName == "Ama Fh" ~ 44,
TRUE ~ 80
)
The way of doing this in base R would be to just nest calls to ifelse, but this quickly becomes unreadable.
What would you do if they are 2 that are differents Ama Fh 11 0 44, Carl Goss 69 360 40 and the rest 80, sorry I just edited my post. Thanks by the way
– luis vergara
Nov 13 '18 at 6:41
Error: unexpected '=' in "Exp$Budget <- Exp$Budget + ifelse(Exp$taskStaffName ="
– luis vergara
Nov 13 '18 at 6:46
Sorry, I keep doing this. You need to use==as the equality operator. Keep in mind that much of the code posted to Stack Overflow hasn't (and sometimes can't be) tested.
– Tim Biegeleisen
Nov 13 '18 at 6:47
Great is working! Thanks!
– luis vergara
Nov 13 '18 at 6:53
add a comment |
Yes, we can try using ifelse here:
df$Budget <- ifelse(df$taskStaffName == "Carl Goss", 40, 80)
Assuming you actually wanted to add a value to the budget column, then use:
df$Budget <- df$Budget + ifelse(df$taskStaffName == "Carl Goss", 40, 80)
To handle more than two uses cases, we can try using case_when from the dplyr package:
df$Budget <- case_when(
df$taskStaffName == "Carl Goss" ~ 40,
df$taskStaffName == "Ama Fh" ~ 44,
TRUE ~ 80
)
The way of doing this in base R would be to just nest calls to ifelse, but this quickly becomes unreadable.
What would you do if they are 2 that are differents Ama Fh 11 0 44, Carl Goss 69 360 40 and the rest 80, sorry I just edited my post. Thanks by the way
– luis vergara
Nov 13 '18 at 6:41
Error: unexpected '=' in "Exp$Budget <- Exp$Budget + ifelse(Exp$taskStaffName ="
– luis vergara
Nov 13 '18 at 6:46
Sorry, I keep doing this. You need to use==as the equality operator. Keep in mind that much of the code posted to Stack Overflow hasn't (and sometimes can't be) tested.
– Tim Biegeleisen
Nov 13 '18 at 6:47
Great is working! Thanks!
– luis vergara
Nov 13 '18 at 6:53
add a comment |
Yes, we can try using ifelse here:
df$Budget <- ifelse(df$taskStaffName == "Carl Goss", 40, 80)
Assuming you actually wanted to add a value to the budget column, then use:
df$Budget <- df$Budget + ifelse(df$taskStaffName == "Carl Goss", 40, 80)
To handle more than two uses cases, we can try using case_when from the dplyr package:
df$Budget <- case_when(
df$taskStaffName == "Carl Goss" ~ 40,
df$taskStaffName == "Ama Fh" ~ 44,
TRUE ~ 80
)
The way of doing this in base R would be to just nest calls to ifelse, but this quickly becomes unreadable.
Yes, we can try using ifelse here:
df$Budget <- ifelse(df$taskStaffName == "Carl Goss", 40, 80)
Assuming you actually wanted to add a value to the budget column, then use:
df$Budget <- df$Budget + ifelse(df$taskStaffName == "Carl Goss", 40, 80)
To handle more than two uses cases, we can try using case_when from the dplyr package:
df$Budget <- case_when(
df$taskStaffName == "Carl Goss" ~ 40,
df$taskStaffName == "Ama Fh" ~ 44,
TRUE ~ 80
)
The way of doing this in base R would be to just nest calls to ifelse, but this quickly becomes unreadable.
edited Nov 13 '18 at 6:46
answered Nov 13 '18 at 6:36
Tim BiegeleisenTim Biegeleisen
219k1388140
219k1388140
What would you do if they are 2 that are differents Ama Fh 11 0 44, Carl Goss 69 360 40 and the rest 80, sorry I just edited my post. Thanks by the way
– luis vergara
Nov 13 '18 at 6:41
Error: unexpected '=' in "Exp$Budget <- Exp$Budget + ifelse(Exp$taskStaffName ="
– luis vergara
Nov 13 '18 at 6:46
Sorry, I keep doing this. You need to use==as the equality operator. Keep in mind that much of the code posted to Stack Overflow hasn't (and sometimes can't be) tested.
– Tim Biegeleisen
Nov 13 '18 at 6:47
Great is working! Thanks!
– luis vergara
Nov 13 '18 at 6:53
add a comment |
What would you do if they are 2 that are differents Ama Fh 11 0 44, Carl Goss 69 360 40 and the rest 80, sorry I just edited my post. Thanks by the way
– luis vergara
Nov 13 '18 at 6:41
Error: unexpected '=' in "Exp$Budget <- Exp$Budget + ifelse(Exp$taskStaffName ="
– luis vergara
Nov 13 '18 at 6:46
Sorry, I keep doing this. You need to use==as the equality operator. Keep in mind that much of the code posted to Stack Overflow hasn't (and sometimes can't be) tested.
– Tim Biegeleisen
Nov 13 '18 at 6:47
Great is working! Thanks!
– luis vergara
Nov 13 '18 at 6:53
What would you do if they are 2 that are differents Ama Fh 11 0 44, Carl Goss 69 360 40 and the rest 80, sorry I just edited my post. Thanks by the way
– luis vergara
Nov 13 '18 at 6:41
What would you do if they are 2 that are differents Ama Fh 11 0 44, Carl Goss 69 360 40 and the rest 80, sorry I just edited my post. Thanks by the way
– luis vergara
Nov 13 '18 at 6:41
Error: unexpected '=' in "Exp$Budget <- Exp$Budget + ifelse(Exp$taskStaffName ="
– luis vergara
Nov 13 '18 at 6:46
Error: unexpected '=' in "Exp$Budget <- Exp$Budget + ifelse(Exp$taskStaffName ="
– luis vergara
Nov 13 '18 at 6:46
Sorry, I keep doing this. You need to use
== as the equality operator. Keep in mind that much of the code posted to Stack Overflow hasn't (and sometimes can't be) tested.– Tim Biegeleisen
Nov 13 '18 at 6:47
Sorry, I keep doing this. You need to use
== as the equality operator. Keep in mind that much of the code posted to Stack Overflow hasn't (and sometimes can't be) tested.– Tim Biegeleisen
Nov 13 '18 at 6:47
Great is working! Thanks!
– luis vergara
Nov 13 '18 at 6:53
Great is working! Thanks!
– luis vergara
Nov 13 '18 at 6:53
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53275106%2fcreate-a-new-column-based-in-if-statments-r%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