creating a categorical variable

hello, I'm trying to create a new categorical variable based on percentiles higher than 25 but lower than 50 but dont know ho to express that in the line of code, here is the one I have so far.

income.type <- case_when(dataset$income == .25~ 1,
dataset$income >= .25 but <= .75 ~ 2,
dataset$income>= .50 but <= .75 ~ 3,
dataset$income == .75~ 4=)

You can use the AND operator, &, to express the logic.

income.type <- case_when(dataset$income <= .25~ 1,
dataset$income > .25 & dataset$income <= .5 ~ 2,
dataset$income> .50 & dataset$income <= .75 ~ 3,
dataset$income > .75~ 4=)

Notice that I changed the comparisons. You should not have a condition for == 0.25 and another one that starts with >= 0.25. Those will both be TRUE for values of 0.25. I merely guessed at a possible arrangement of the logic and you should change that to whatever is correct for your situation.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.