Page 1 of 2

Computing variables with If-Then

Posted: Thu Apr 29, 2021 6:00 pm
by DSK
It seems like I should be able to do this, but I can't seem to figure it out. Suppose I want to compute a new variable based on 2 existing variables, something like this:

If A=1 and B>9 then C=2
If A=1 and (B<10 and B>5) then C=1
Else C=0

Can someone tell me how to do this in Jamovi? Thanks!

Re: Computing variables with If-Then

Posted: Fri Apr 30, 2021 12:00 am
by jonathon
i can't see anything wrong with those rules. if they're not working the way you expect, attach a screenshot.

cheers

jonathon

Re: Computing variables with If-Then

Posted: Fri Apr 30, 2021 11:33 am
by MAgojam
Hi, @DSK.
Do you think the suggestion of this screenshot is what you are looking for?
IF_THEN_ELSE.PNG
IF_THEN_ELSE.PNG (46.22 KiB) Viewed 12457 times
Cheers,
Maurizio

Re: Computing variables with If-Then

Posted: Thu Mar 23, 2023 11:07 am
by flatliner
Hello,

I want to compute a new variable with two calculations like: IF var_1 = "high" then 7- var_2

I hope you can help.

Thanks!

Re: Computing variables with If-Then

Posted: Thu Mar 23, 2023 1:47 pm
by MAgojam
Hi @flatliner,
you haven't indicated what the argument will be if the test with IF(test, TRUE, FALSE) results in FALSE.
Anyway I look at the screenshot, where with test TRUE it returns 7-var_2, while with FALSE it returns var_2.
ScreenShot_20230323144253.jpeg
ScreenShot_20230323144253.jpeg (145.36 KiB) Viewed 9359 times
Cheers,
Maurizio

Re: Computing variables with If-Then

Posted: Thu Mar 23, 2023 1:51 pm
by flatliner
Hi Maurizio,
Thanks a lot, that is exactly what I was looking for and it works!!!

Re: Computing variables with If-Then

Posted: Mon Jun 12, 2023 3:23 pm
by erosenzweig
Hi All -- Found this thread and tried the syntax structure but it is telling me that my formula is mis-specified? What am I missing here? This is a super simple one -- compute a new variable, and if this other variable = 1, then new variable =1. If other variable is anything else then new variable = 0.
I thought I had the syntax right -- IF (cancertreat=1,1,0)?

Link to image below...

https://snipboard.io/J3p8LG.jpg

Re: Computing variables with If-Then

Posted: Mon Jun 12, 2023 6:50 pm
by MAgojam
Hey @erosenzweig,
if you take a look at the screenshots in the posts above, you will see that you need == (you only used one) for the formula to be correct and get the result you are looking for.

Cheers,
Maurizio

Re: Computing variables with If-Then

Posted: Mon Jun 12, 2023 7:44 pm
by erosenzweig
Aaaah thank you! I stared at that for so long and still didn't see it. Appreciated.

Re: Computing variables with If-Then

Posted: Mon Oct 09, 2023 2:33 pm
by nebojsa
Hi Jonathon!
I have data on one variable (name: L_CL_CR_R with 4 categories) collected in YEAR = 1 and 3. I want to compute the variable based on previous two with the function IF and. This is my formula:
IF(YEAR==1 and L_CL_CR_R==1, 1, IF(YEAR==3 and L_CL_CR_R==1, 2, IF(YEAR==1 and L_CL_CR_R==2, 3, IF(YEAR==3 and L_CL_CR_R==2, 4, IF(YEAR==1 and L_CL_CR_R==3, 5, IF(YEAR==3 and L_CL_CR_R==3, 6, IF(YEAR==1 aJnd L_CL_CR_R==4, 7, IF(YEAR==3 and L_CL_CR_R==4, 8, 0))
Jamovi sais it is mis-specified. Where is the mistake?
Thank you!