Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

Ifelse function with multiple conditional Outputs [duplicate]

$
0
0

Let X be the vector 1:100. I want to create a new vector that assigns the text “Under 20” for numbers that are below 20 in X. I also want the new vector to assign the text “Over 80” for numbers that are above 80 in X. Else, I want the vector to say “Between 20 and 80”

How do I do this? The following code doesn’t works because the logic gets rewritten:

Y = ifelse( X < 20, “Below 20”, “Between 20 and 80”) Y = ifelse( X > 80, “Above 80”, “Between 20 and 80”) Y

But this replaces all “Below 20” with “Between 20 and 80” because the 2nd ifelse statement checks the ones that are below 20 as well and replaces “Below 20” with “Between 20 and 80”

I also tried the following but this spits out all of them to be “Below 20”:

Y = ifelse( X< 20, “Below 20” , ifelse( X > 80, “Above 80”, “Between 20 and 80”) )

My question is, how do I produce the vector I want. Preferably in the base package. I tried other re-arrangements of the code as well with no luck.

That is, Are there nested if else statements for logic on vectors? Thank you in advance for your help.


Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>