简体   繁体   English

在R中将If-Else与多个do语句链接

[英]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. 首先,我知道SO中有很多关于R中的if / else语句的问题,但是没有一个问题对我的具体情况有所帮助,并且我已经为此苦苦挣扎了一段时间。

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). 我需要基于metricx的值(风险和答案)创建两个新变量。

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. 我的理解是,每次调用else时,R必须遍历每条记录,因此链式选项会更好。

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? 我的问题:1.为什么我的解决方案不起作用? They appear to follow the syntax I have seen on the R site? 它们似乎遵循我在R网站上看到的语法? 2. What is the correct way to achieve my desired output? 2.实现所需输出的正确方法是什么?

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)

I think using dplyr this is what you want, right? 我认为使用dplyr就是您想要的,对吧?

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"))

Per definition you'll always have an answer, which is why I left df$answer out. 根据定义,您将始终有一个答案,这就是为什么我不使用df $ answer的原因。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM