0

I am using R Studio and I have variables that are somewhere between 0 and 1.5 and I want to change them to read either YES, if they are above 0.5, and NO, if they are below 0.5. Would I use ifelse() to do this or is there a better way?

Gray C
  • 1
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. `ifelse` is a good choice: `ifelse(x>.5, "Yes","No")`. Not sure what a "better" solution would mean in this case. – MrFlick May 11 '21 at 01:30
  • Sorry about that, I am using this h09$GDP.per.capita <- ifelse(h09$GDP.per.capita >= 0.5, h09$GDP.per.capita == "YES", h09$GDP.per.capita == "NO") but it is returning a FALSE value for each value, I am definitely doing something wrong I think – Gray C May 11 '21 at 01:45
  • It should look more like `h09$GDP.per.capita <- ifelse(h09$GDP.per.capita >= 0.5, "YES", "NO")` – MrFlick May 11 '21 at 01:46
  • That worked, thank you so much! You are the best! – Gray C May 11 '21 at 01:56
  • without using `ifelse` you may do this.. `c('No', 'Yes')[1 + (h09$GDP.per.capita >= 0.5)]` and store/assign it to `h09$GDP.per.capita` – AnilGoyal May 11 '21 at 07:04

0 Answers0