Computing variables with If-Then

General help and assistance with jamovi. Bug reports can be made at our issues page: https://github.com/jamovi/jamovi/issues . (If you're unsure feel free to discuss it here)
DSK
Posts: 5
Joined: Mon May 25, 2020 9:34 pm

Computing variables with If-Then

Post 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!
User avatar
jonathon
Posts: 2613
Joined: Fri Jan 27, 2017 10:04 am

Re: Computing variables with If-Then

Post 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
User avatar
MAgojam
Posts: 421
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Computing variables with If-Then

Post 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 8202 times
Cheers,
Maurizio
flatliner
Posts: 6
Joined: Sun Feb 26, 2023 2:18 pm

Re: Computing variables with If-Then

Post 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!
User avatar
MAgojam
Posts: 421
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Computing variables with If-Then

Post 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 5104 times
Cheers,
Maurizio
flatliner
Posts: 6
Joined: Sun Feb 26, 2023 2:18 pm

Re: Computing variables with If-Then

Post by flatliner »

Hi Maurizio,
Thanks a lot, that is exactly what I was looking for and it works!!!
erosenzweig
Posts: 3
Joined: Thu Mar 09, 2023 3:31 am

Re: Computing variables with If-Then

Post 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
User avatar
MAgojam
Posts: 421
Joined: Thu Jun 08, 2017 2:33 pm
Location: Parma (Italy)

Re: Computing variables with If-Then

Post 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
erosenzweig
Posts: 3
Joined: Thu Mar 09, 2023 3:31 am

Re: Computing variables with If-Then

Post by erosenzweig »

Aaaah thank you! I stared at that for so long and still didn't see it. Appreciated.
nebojsa
Posts: 5
Joined: Sun Sep 17, 2023 9:01 pm

Re: Computing variables with If-Then

Post 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!
Post Reply