Reputation: 11
I created two variables "score_diff" and "seed_diff", I would like to create a binary variable which is defined by if the score_diff is <0 and if the seed_diff is greater than 5.
df$score_diff <- df$score1- df$score2
df$seed_diff <- df$seed1- df$seed2
I tried this, however, it didn't work, I am new to this so bear with me:
df$upset <- ifelse(df$score_diff<0 & df$seed_diff>5)
Upvotes: 1
Views: 980
Reputation: 3184
As others have explained, your code only specifies the "test" (df$score_diff < 0 & df$seed_diff > 5
) part of an ifelse
statement. If you want to convert it to an integer
with values 0,1
you can use as.integer
.
df$upset <- as.integer(df$score_diff < 0 & df$seed_diff > 5)
Upvotes: 0
Reputation: 1560
Your code doesn't work, cos you only set the condition, and not what should happen in case the condition is TRUE or FALSE. So, the second argument 1
, says that in case the condition is TRUE, the value is 1; and the third argument specifies what should happen if the condition is FALSE. Simplified: ifelse(test, yes, no)
.
df$upset <- ifelse(df$score_diff<0 & df$seed_diff>5, 1, 0)
Upvotes: 1