If else statements to










-1















I have some trouble creating new columns from existing columns.



What I want to achieve is:



1) For rows where treatment equals "mixed", I want to locate the NA and then;



2a) If the NA is under pun1 or pun2 then;



the integer of eva3 and eva4 must be converted/copied to eva_out1 and eva_out2, respectively,



2b) Vice versa, if NA is under pun3 or pun4, then;



eva1 equals to eva_out1, where eva2 also has its integer converted to eva_out2



We will never have eva_out3 because of the dyadic group composition.



At first I tried this:



df5$eva_out1 <- with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5$eva_out1 <- eva3,NA ))


Which gets most of the job done, but I can't run this multiple times, because other values would be overwritten by the other block of code that goes up for eva_out2.



Then I tried this:



 df5$eva_out1 <- with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5$eva_out1 == eva2 & df5$eva_out2 == eva3, ifelse(
(is.na(pun3) | is.na(pun4)) & treatment == "mixed",
df5$eva_out1 == eva1 & df5$eva_out2 == eva2, NA )))


And this



if(df5$treatment == "mixed") 
if ( is.na(pun1) | is.na(pun2) )
eva_out1 <- eva3 & eva_out2 <- eva4
else if ( is.na(pun3) | is.na(pun4) )
eva_out1 <- eva1 & eva_out2 <- eva2
else
eva_out1 <- NULL & eva_out2 <- NULL



But both either spew errors or do not give the results I need.
I've been looking at functions, but ifelse() seems more legit in this situation. Correct me if I'm wrong though.



First 12 rows of the data:



. UniqueSS subject group part round treatment pun1 pun2 pun3 pun4 eva1 eva2 eva3
1 11 1 1 punishment 0 homogenous NA 0 0 0 0 0 0
2 12 2 1 punishment 0 homogenous 0 NA 0 0 0 0 0
3 13 3 1 punishment 0 homogenous 0 0 NA 0 0 0 1
4 14 4 1 punishment 0 homogenous 0 0 1 NA 0 0 0
5 11 1 1 punishment 1 homogenous NA 0 0 0 0 0 0
6 12 2 1 punishment 1 homogenous 0 NA 0 0 0 0 0
7 13 3 1 punishment 1 homogenous 0 0 NA 0 0 0 0
8 14 4 1 punishment 1 homogenous 0 0 0 NA 0 0 0
9 11 1 1 punishment 2 homogenous NA 0 0 0 0 0 0
10 12 2 1 punishment 2 homogenous 0 NA 0 0 0 0 0
11 13 3 1 punishment 2 homogenous 0 0 NA 0 0 0 0
12 14 4 1 punishment 2 homogenous 0 0 0 NA 0 0 0


For the third row we would have columns eva_out1 == 0, eva_out2 == 0, because member pun4 is in the same category group as pun3 and thus may not be converted to another column.



Thanks in advance!










share|improve this question



















  • 5





    Not knowing R, I assume all the code shown is R. What does this question have to do with Python?

    – chepner
    Nov 14 '18 at 14:26











  • @chepner It got the tag by accident, my bad

    – fleems
    Nov 20 '18 at 18:53















-1















I have some trouble creating new columns from existing columns.



What I want to achieve is:



1) For rows where treatment equals "mixed", I want to locate the NA and then;



2a) If the NA is under pun1 or pun2 then;



the integer of eva3 and eva4 must be converted/copied to eva_out1 and eva_out2, respectively,



2b) Vice versa, if NA is under pun3 or pun4, then;



eva1 equals to eva_out1, where eva2 also has its integer converted to eva_out2



We will never have eva_out3 because of the dyadic group composition.



At first I tried this:



df5$eva_out1 <- with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5$eva_out1 <- eva3,NA ))


Which gets most of the job done, but I can't run this multiple times, because other values would be overwritten by the other block of code that goes up for eva_out2.



Then I tried this:



 df5$eva_out1 <- with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5$eva_out1 == eva2 & df5$eva_out2 == eva3, ifelse(
(is.na(pun3) | is.na(pun4)) & treatment == "mixed",
df5$eva_out1 == eva1 & df5$eva_out2 == eva2, NA )))


And this



if(df5$treatment == "mixed") 
if ( is.na(pun1) | is.na(pun2) )
eva_out1 <- eva3 & eva_out2 <- eva4
else if ( is.na(pun3) | is.na(pun4) )
eva_out1 <- eva1 & eva_out2 <- eva2
else
eva_out1 <- NULL & eva_out2 <- NULL



But both either spew errors or do not give the results I need.
I've been looking at functions, but ifelse() seems more legit in this situation. Correct me if I'm wrong though.



First 12 rows of the data:



. UniqueSS subject group part round treatment pun1 pun2 pun3 pun4 eva1 eva2 eva3
1 11 1 1 punishment 0 homogenous NA 0 0 0 0 0 0
2 12 2 1 punishment 0 homogenous 0 NA 0 0 0 0 0
3 13 3 1 punishment 0 homogenous 0 0 NA 0 0 0 1
4 14 4 1 punishment 0 homogenous 0 0 1 NA 0 0 0
5 11 1 1 punishment 1 homogenous NA 0 0 0 0 0 0
6 12 2 1 punishment 1 homogenous 0 NA 0 0 0 0 0
7 13 3 1 punishment 1 homogenous 0 0 NA 0 0 0 0
8 14 4 1 punishment 1 homogenous 0 0 0 NA 0 0 0
9 11 1 1 punishment 2 homogenous NA 0 0 0 0 0 0
10 12 2 1 punishment 2 homogenous 0 NA 0 0 0 0 0
11 13 3 1 punishment 2 homogenous 0 0 NA 0 0 0 0
12 14 4 1 punishment 2 homogenous 0 0 0 NA 0 0 0


For the third row we would have columns eva_out1 == 0, eva_out2 == 0, because member pun4 is in the same category group as pun3 and thus may not be converted to another column.



Thanks in advance!










share|improve this question



















  • 5





    Not knowing R, I assume all the code shown is R. What does this question have to do with Python?

    – chepner
    Nov 14 '18 at 14:26











  • @chepner It got the tag by accident, my bad

    – fleems
    Nov 20 '18 at 18:53













-1












-1








-1


1






I have some trouble creating new columns from existing columns.



What I want to achieve is:



1) For rows where treatment equals "mixed", I want to locate the NA and then;



2a) If the NA is under pun1 or pun2 then;



the integer of eva3 and eva4 must be converted/copied to eva_out1 and eva_out2, respectively,



2b) Vice versa, if NA is under pun3 or pun4, then;



eva1 equals to eva_out1, where eva2 also has its integer converted to eva_out2



We will never have eva_out3 because of the dyadic group composition.



At first I tried this:



df5$eva_out1 <- with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5$eva_out1 <- eva3,NA ))


Which gets most of the job done, but I can't run this multiple times, because other values would be overwritten by the other block of code that goes up for eva_out2.



Then I tried this:



 df5$eva_out1 <- with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5$eva_out1 == eva2 & df5$eva_out2 == eva3, ifelse(
(is.na(pun3) | is.na(pun4)) & treatment == "mixed",
df5$eva_out1 == eva1 & df5$eva_out2 == eva2, NA )))


And this



if(df5$treatment == "mixed") 
if ( is.na(pun1) | is.na(pun2) )
eva_out1 <- eva3 & eva_out2 <- eva4
else if ( is.na(pun3) | is.na(pun4) )
eva_out1 <- eva1 & eva_out2 <- eva2
else
eva_out1 <- NULL & eva_out2 <- NULL



But both either spew errors or do not give the results I need.
I've been looking at functions, but ifelse() seems more legit in this situation. Correct me if I'm wrong though.



First 12 rows of the data:



. UniqueSS subject group part round treatment pun1 pun2 pun3 pun4 eva1 eva2 eva3
1 11 1 1 punishment 0 homogenous NA 0 0 0 0 0 0
2 12 2 1 punishment 0 homogenous 0 NA 0 0 0 0 0
3 13 3 1 punishment 0 homogenous 0 0 NA 0 0 0 1
4 14 4 1 punishment 0 homogenous 0 0 1 NA 0 0 0
5 11 1 1 punishment 1 homogenous NA 0 0 0 0 0 0
6 12 2 1 punishment 1 homogenous 0 NA 0 0 0 0 0
7 13 3 1 punishment 1 homogenous 0 0 NA 0 0 0 0
8 14 4 1 punishment 1 homogenous 0 0 0 NA 0 0 0
9 11 1 1 punishment 2 homogenous NA 0 0 0 0 0 0
10 12 2 1 punishment 2 homogenous 0 NA 0 0 0 0 0
11 13 3 1 punishment 2 homogenous 0 0 NA 0 0 0 0
12 14 4 1 punishment 2 homogenous 0 0 0 NA 0 0 0


For the third row we would have columns eva_out1 == 0, eva_out2 == 0, because member pun4 is in the same category group as pun3 and thus may not be converted to another column.



Thanks in advance!










share|improve this question
















I have some trouble creating new columns from existing columns.



What I want to achieve is:



1) For rows where treatment equals "mixed", I want to locate the NA and then;



2a) If the NA is under pun1 or pun2 then;



the integer of eva3 and eva4 must be converted/copied to eva_out1 and eva_out2, respectively,



2b) Vice versa, if NA is under pun3 or pun4, then;



eva1 equals to eva_out1, where eva2 also has its integer converted to eva_out2



We will never have eva_out3 because of the dyadic group composition.



At first I tried this:



df5$eva_out1 <- with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5$eva_out1 <- eva3,NA ))


Which gets most of the job done, but I can't run this multiple times, because other values would be overwritten by the other block of code that goes up for eva_out2.



Then I tried this:



 df5$eva_out1 <- with(df5, ifelse(
(is.na(pun1) | is.na(pun2)) & treatment == "mixed",
df5$eva_out1 == eva2 & df5$eva_out2 == eva3, ifelse(
(is.na(pun3) | is.na(pun4)) & treatment == "mixed",
df5$eva_out1 == eva1 & df5$eva_out2 == eva2, NA )))


And this



if(df5$treatment == "mixed") 
if ( is.na(pun1) | is.na(pun2) )
eva_out1 <- eva3 & eva_out2 <- eva4
else if ( is.na(pun3) | is.na(pun4) )
eva_out1 <- eva1 & eva_out2 <- eva2
else
eva_out1 <- NULL & eva_out2 <- NULL



But both either spew errors or do not give the results I need.
I've been looking at functions, but ifelse() seems more legit in this situation. Correct me if I'm wrong though.



First 12 rows of the data:



. UniqueSS subject group part round treatment pun1 pun2 pun3 pun4 eva1 eva2 eva3
1 11 1 1 punishment 0 homogenous NA 0 0 0 0 0 0
2 12 2 1 punishment 0 homogenous 0 NA 0 0 0 0 0
3 13 3 1 punishment 0 homogenous 0 0 NA 0 0 0 1
4 14 4 1 punishment 0 homogenous 0 0 1 NA 0 0 0
5 11 1 1 punishment 1 homogenous NA 0 0 0 0 0 0
6 12 2 1 punishment 1 homogenous 0 NA 0 0 0 0 0
7 13 3 1 punishment 1 homogenous 0 0 NA 0 0 0 0
8 14 4 1 punishment 1 homogenous 0 0 0 NA 0 0 0
9 11 1 1 punishment 2 homogenous NA 0 0 0 0 0 0
10 12 2 1 punishment 2 homogenous 0 NA 0 0 0 0 0
11 13 3 1 punishment 2 homogenous 0 0 NA 0 0 0 0
12 14 4 1 punishment 2 homogenous 0 0 0 NA 0 0 0


For the third row we would have columns eva_out1 == 0, eva_out2 == 0, because member pun4 is in the same category group as pun3 and thus may not be converted to another column.



Thanks in advance!







r var






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 11:23







fleems

















asked Nov 14 '18 at 14:23









fleemsfleems

24117




24117







  • 5





    Not knowing R, I assume all the code shown is R. What does this question have to do with Python?

    – chepner
    Nov 14 '18 at 14:26











  • @chepner It got the tag by accident, my bad

    – fleems
    Nov 20 '18 at 18:53












  • 5





    Not knowing R, I assume all the code shown is R. What does this question have to do with Python?

    – chepner
    Nov 14 '18 at 14:26











  • @chepner It got the tag by accident, my bad

    – fleems
    Nov 20 '18 at 18:53







5




5





Not knowing R, I assume all the code shown is R. What does this question have to do with Python?

– chepner
Nov 14 '18 at 14:26





Not knowing R, I assume all the code shown is R. What does this question have to do with Python?

– chepner
Nov 14 '18 at 14:26













@chepner It got the tag by accident, my bad

– fleems
Nov 20 '18 at 18:53





@chepner It got the tag by accident, my bad

– fleems
Nov 20 '18 at 18:53












1 Answer
1






active

oldest

votes


















2














Not sure I fully understand but is this what you're after?



df5$eva_out_1 <- df5$eva_out_2 <- NA

cond1 <- df5$treatment == "mixed" & (is.na(df5$pun1) | is.na(df5$pun2))
df5$eva_out_1[cond1] <- df5$eva3[cond1]
df5$eva_out_2[cond1] <- df5$eva4[cond1]

cond2 <- df5$treatment == "mixed" & (is.na(df5$pun3) | is.na(df5$pun4))
df5$eva_out_1[cond2] <- df5$eva1[cond2]
df5$eva_out_2[cond2] <- df5$eva2[cond2]





share|improve this answer























  • Hi, I had to rearrange the data to make it work, but I managed. Thanks, it was a really weird problem to solve :)

    – fleems
    Nov 20 '18 at 18:53










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%2f53302420%2fif-else-statements-to%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









2














Not sure I fully understand but is this what you're after?



df5$eva_out_1 <- df5$eva_out_2 <- NA

cond1 <- df5$treatment == "mixed" & (is.na(df5$pun1) | is.na(df5$pun2))
df5$eva_out_1[cond1] <- df5$eva3[cond1]
df5$eva_out_2[cond1] <- df5$eva4[cond1]

cond2 <- df5$treatment == "mixed" & (is.na(df5$pun3) | is.na(df5$pun4))
df5$eva_out_1[cond2] <- df5$eva1[cond2]
df5$eva_out_2[cond2] <- df5$eva2[cond2]





share|improve this answer























  • Hi, I had to rearrange the data to make it work, but I managed. Thanks, it was a really weird problem to solve :)

    – fleems
    Nov 20 '18 at 18:53















2














Not sure I fully understand but is this what you're after?



df5$eva_out_1 <- df5$eva_out_2 <- NA

cond1 <- df5$treatment == "mixed" & (is.na(df5$pun1) | is.na(df5$pun2))
df5$eva_out_1[cond1] <- df5$eva3[cond1]
df5$eva_out_2[cond1] <- df5$eva4[cond1]

cond2 <- df5$treatment == "mixed" & (is.na(df5$pun3) | is.na(df5$pun4))
df5$eva_out_1[cond2] <- df5$eva1[cond2]
df5$eva_out_2[cond2] <- df5$eva2[cond2]





share|improve this answer























  • Hi, I had to rearrange the data to make it work, but I managed. Thanks, it was a really weird problem to solve :)

    – fleems
    Nov 20 '18 at 18:53













2












2








2







Not sure I fully understand but is this what you're after?



df5$eva_out_1 <- df5$eva_out_2 <- NA

cond1 <- df5$treatment == "mixed" & (is.na(df5$pun1) | is.na(df5$pun2))
df5$eva_out_1[cond1] <- df5$eva3[cond1]
df5$eva_out_2[cond1] <- df5$eva4[cond1]

cond2 <- df5$treatment == "mixed" & (is.na(df5$pun3) | is.na(df5$pun4))
df5$eva_out_1[cond2] <- df5$eva1[cond2]
df5$eva_out_2[cond2] <- df5$eva2[cond2]





share|improve this answer













Not sure I fully understand but is this what you're after?



df5$eva_out_1 <- df5$eva_out_2 <- NA

cond1 <- df5$treatment == "mixed" & (is.na(df5$pun1) | is.na(df5$pun2))
df5$eva_out_1[cond1] <- df5$eva3[cond1]
df5$eva_out_2[cond1] <- df5$eva4[cond1]

cond2 <- df5$treatment == "mixed" & (is.na(df5$pun3) | is.na(df5$pun4))
df5$eva_out_1[cond2] <- df5$eva1[cond2]
df5$eva_out_2[cond2] <- df5$eva2[cond2]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 14:48









Milan ValášekMilan Valášek

36319




36319












  • Hi, I had to rearrange the data to make it work, but I managed. Thanks, it was a really weird problem to solve :)

    – fleems
    Nov 20 '18 at 18:53

















  • Hi, I had to rearrange the data to make it work, but I managed. Thanks, it was a really weird problem to solve :)

    – fleems
    Nov 20 '18 at 18:53
















Hi, I had to rearrange the data to make it work, but I managed. Thanks, it was a really weird problem to solve :)

– fleems
Nov 20 '18 at 18:53





Hi, I had to rearrange the data to make it work, but I managed. Thanks, it was a really weird problem to solve :)

– fleems
Nov 20 '18 at 18:53



















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%2f53302420%2fif-else-statements-to%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







這個網誌中的熱門文章

What does pagestruct do in Eviews?

Dutch intervention in Lombok and Karangasem

Channel Islands