#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

# Compute and re-code into new variables in JASP

Hi.

Could somebody help me🙏.

How do I go about to:

a) create a new nominal (binary) variable from a scale variable. So, I have an age variable (denoted as year of age) and I want to dichotomize this to a nominal variable with two levels, thus, under or equal to the mean age =1 and over the mean age = 2.

b) collapse an ordinal scale (Likert) with five steps to an ordinal scale with three steps. Thus, I want 1 & 2 =1, 3=2 and 4 & 5 =3.

Grateful for some help :-)

Per

• For others who may land here: in the meantime this was addressed at https://github.com/jasp-stats/jasp-issues/issues/1844

E.J.

• edited October 2023

You helped me earlier on the GitHub post a above (it is closed now) as I wanted to create a new variable where I cluster 5-level data to 3-level data. Thus, 1 & 2 =1, 3=2 and 4 & 5 =3.

You gave me a great tip with the following R code (that I have used so many times since, despite I hate syntax and coding):

ifElse(facFive <= 2, 1,

ifElse(facFive <= 3, 2, 3)

)

Could you just explain the mechanics behind the codes. I can’t get my head around it. In my logic I ask the program to:

• If the variable is less or equal to 2, make it a 1
• If the variable is equal to 3, make it a 2
• If the variable is more or equal to 4, make it a 3

Could you explain the like I was a kid?🤪🙏🏻

All the best

Per

• Hi Per,

No worries! The key is to realize that the two statements are nested, so it is actually one statement like so:

`ifElse(facFive <= 2, 1, ifElse(facFive <= 3, 2, 3))`

We can also see this as a decision tree, like below:

You start at the top and ask the question, is this value <= 2? If yes, you go left, and if no you go right. If you go left, you end up with 1 and are done. If no, you ask another question, is this value <= 3? If yes, you go left and end up at 2. If no then you go right and end up at 3. In general, you repeat this until there are no more questions and you are at a value. We repeat this process for each row in the dataset.

I hope this makes things more understandable, but let me know if anything else is unclear!

Don

• Thanks Don🙏🏻. That was extremely helpful😍.

All the best

Per😎

• Hi, I just posted:

Do you know how to recode a variable into 2 variables? In SPSS that option is easy (Transform, then, Recode Into Diff Variables) but I'm having trouble finding that option in JASP. One way could be creating a new variable and coding the values as I need them with R code?

Could I use this code to solve my question?

Many thanks in advance.

• Hi Ananda,

You could create multiple computed columns, one for each new variable you want.

If that is not what you meant, could you provide an example?

Best,

Don

• Hi, I'm so sorry for replying so late. I honestly lost hope of somebody helping me with this and can't use the GitHub page for now. Let's start from the beginning.

I have a variable named LonelinessSUM because it was computed in JASP to have the totals per participant of the short loneliness scale where the minimum score is 3=nonlonely and the max score is 9=severely lonely. When creating a categorical variable with the values 0= for 3,4,5 = nonlonely; and, 1 for 6,7,9 = Lonely was "easy" as I did it manually and it worked. But, now, I need those numbers to be transformed into a categorical variable where 3,4,5 means NonLonely and 7,8,9 means Severely Lonely. This means skipping the scores that are a 6 as I need to go for the extremes for the second round of ANOVAs (repeated measures). How do I do that in JASP?

Don't know if I'm making sense here.

• But, now, I need those numbers to be transformed into a categorical variable where 3,4,5 means NonLonely and 7,8,9 means Severely Lonely. This means skipping the scores that are a 6 as I need to go for the extremes for the second round of ANOVAs (repeated measures). How do I do that in JASP?

I would do the following:

and then filter out the missing values before running the analysis.

• THANK YOU SO MUCH!

I'll give it a try after dinner and let you know!

• I've tried following

ifelse(LonelinessTOTAL ≥ 3 & LonelinessTOTAL ≤ 5,

"NonLonely",

ifelse(LonelinessTOTAL ≥ 7 & LonelinessTOTAL ≤ 9,

"Lonely,

NA)

)

But it gives me the following error:

Error: Error in parse(text = .rCode): <text>:1:61: unexpected input

1: local({local({;calcedVals <- {ifelse(LonelinessTOTAL ≥

What am I doing wrong?

• Hi, thank you so much for your help. The code script you gave me didn't work exactly that way but with a lot of help I changed a couple of things and it worked. I'm using the following script:

LonelinessExtremes <- ifelse(LonelinessTOTAL >= 3 & LonelinessTOTAL <= 5,

"NonLonely",

ifelse(LonelinessTOTAL > 6 & LonelinessTOTAL <= 9,

"Lonely",

NA))

I'll leave it here in case somebody needs it and maybe if you have access to the GitHub page (I don't for some reason) post it there as well.

Have a lovely week,

Ananda.