在R中将If-Else与多个do语句链接 [英] Chaining If-Else with multiple do statements in R

查看:78
本文介绍了在R中将If-Else与多个do语句链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道SO中有很多关于R中if/else语句的问题,但是这些问题都没有对我的具体情况有所帮助,并且我已经为此苦苦挣扎了一段时间.

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)

我需要根据metricx的值(风险和答案)创建两个新变量.

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

我知道这可行....

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)

但是显然这不是一种优雅或有效的方法,因为我必须做几次(我的数据集非常大,而且我的组比这更多).我的理解是,每次调用else时,R必须遍历每条记录,因此链式选项会更好.

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.

我的问题: 1.为什么我的解决方案不起作用?它们似乎遵循我在R网站上看到的语法? 2.实现所需输出的正确方法是什么?

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)

推荐答案

我认为使用dplyr是您想要的,对吧?

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

这篇关于在R中将If-Else与多个do语句链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆