Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using ifelse in r with multiple returns

Tags:

r

if-statement

I was wondering how to set two or more results from a true ifelse statement.
For example, I would like to set y = 2 and t=3 if x=2. I was thinking the code would look something like this:

x=2 
ifelse(x==2,y=2 & t=3, y=0 & t=0)

however this does not work.

like image 876
Bob M Avatar asked Oct 14 '25 15:10

Bob M


1 Answers

You may use if-statement block:

if(x == 2){
    y = 2
    t = 3
}
else {
    y = 0
    t = 0
}

Alternatively, you can try:

ifelse(x == 2, {y = 2; t = 3;}, {y = 0; t = 0;})
like image 90
student Avatar answered Oct 17 '25 05:10

student



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!