Search code examples
rvar

If else statements to


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!


Solution

  • 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]