pyll
pyll

Reputation: 1754

Chaining If-Else with multiple do statements in R

First of all, I know that there are many questions on SO about if/else statements in R, but none of them has been helpful for my specific situation and I've been struggling with this for a while.

I have a dataframe that looks like this:

metricx <- c(5, 4.8, 4.4, 3.6, 3.2, 2.1, 1.9, .5, .3, .1)
df <- as.data.frame(metricx)

I need to create two new variables based on the value of metricx (risk and answer).

I know this works....

df$risk <- ifelse(df$metricx >= 4.5, 'VERY HIGH', 'HIGH')
df$risk <- ifelse(df$metricx < 3.5, 'MEDIUM', df$risk)
df$risk <- ifelse(df$metricx < 2, 'LOW', df$risk)

But obviously not an elegant or efficient way to do it, since I would have to do this several times (my dataset is very large and i have more groups than this). My understanding is that R has to run through every record each time ifelse is called, so a chained option would be better.

I have tried this...

ifelse(df$metricx >= 4.5,
       (df$risk <- 'VERY HIGH' &
        df$answer <- 'Y')
        , 
ifelse(df$metricx >= 3.5,
       (df$risk = 'HIGH' &
        df$answer = 'Y')
        ,
ifelse(df$metricx >= 2,
        (df$risk = 'MEDIUM' &
        df$answer = 'Y')
        ,
ifelse(df$metricx >= .40,
       (df$risk = 'LOW' &
        df$answer = 'Y')
        ,
(df$risk = 'LOW' &
 df$answer = 'N')
)    
) 
)  
)      

And I have tried this...

if (df$metricx >= 4.5){
  df$risk = 'VERY HIGH'
  df$answer = 'Y'
} else if (df$metricx >= 3.5){
  df$risk = 'HIGH'
  df$answer = 'Y'
} else if (df$metricx >= 2){
  df$risk = 'MEDIUM'
  df$answer = 'Y'
} else if (df$metricx >= .40){
  df$risk = 'LOW'
  df$answer = 'Y'
} else {
  df$risk = 'LOW'
  df$answer = 'N'
}

and they both give different errors, neither of which I can understand. I have looke at several different sites attempting to explain, but still cannot figure out how to do this.

My questions: 1. Why are my solutions not working? They appear to follow the syntax I have seen on the R site? 2. What is the correct way to achieve my desired output?

risk <- c('VERY HIGH', 'VERY HIGH', 'HIGH', 'HIGH', 'MEDIUM', 'MEDIUM', 'LOW', 'LOW', 'LOW', 'LOW') 
answer <- c('Y','Y','Y','Y','Y','Y','Y','Y','Y', 'N')

want <- data.frame(metricx, risk, answer)

Upvotes: 0

Views: 1097

Answers (2)

Edwin
Edwin

Reputation: 3252

I think using dplyr this is what you want, right?

library(dplyr)
df <- df %>% mutate(risk = cut(metricx, c(0, 2, 3.5, 4.5, 6),
                    labels = c("LOW", "MEDIUM", "HIGH", "VERY HIGH"))) %>% 
  mutate(answer = ifelse(metricx < .4, "N", "Y"))

Upvotes: 2

r.user.05apr
r.user.05apr

Reputation: 5456

Per definition you'll always have an answer, which is why I left df$answer out. Try:

metricx <- c(5, 4.8, 4.4, 3.6, 3.2, 2.1, 1.9, .5, .3, .1)
df <- as.data.frame(metricx)

myif<-function(x) {
  if (x<2) y="LOW" else 
    if (x<3.5) y="MEDIUM" else
      if (x<4.5) y="HIGH" else y="VERY HIGH"
  return(y)
}
sapply(df$metricx,myif)

# or:

ifelse(df[1]<2,"LOW",
       ifelse(df[1]<3.5,"MEDIUM",
              ifelse(df[1]<4.5,"HIGH","VERY HIGH")))

# or (modified later):

myif<-function(x) {
  if (x<2) y="LOW" else 
    if (x<3.5) y="MEDIUM" else
      if (x<4.5) y="HIGH" else y="VERY HIGH"
      yv<-c(y,if (x<0.4) "N" else "Y" )
      return(yv)
}
sapply(df$metricx,myif)

Upvotes: 1

Related Questions