3条件语句在r中引发长度错误 [英] 3 conditional statement throws a length error in r

查看:29
本文介绍了3条件语句在r中引发长度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为我拥有的某些数据制定特殊规则(如果值< = 0.1,然后使其丢失,这是一个错误),但是我只想对某些类别执行此操作.

I need to make special rules for some data that I have (if a value is <= 0.1 then make it missing it's an error) but I only want to do it for certain categories.

我的数据看起来像这样

   Category              value
     A                     0.9
     A                     0.001
     A                     0.3
     B                     0.01
     B                     0.8
     C                     0.01
     C                     0.01
     C                     0.2
     C                     NA

我想要这个

   Category              value
     A                     0.9
     A                     0.001
     A                     0.3
     B                     NA
     B                     0.8
     C                     NA
     C                     NA
     C                     0.2
     C                     NA

我的代码如下:

 want<- Mydata %>% 
           mutate(value2= if_else(!is.na(value) &
                                   value<=0.1 & 
                                   Category=='B' ||
                                   !is.na(value) &
                                   value<=0.1 & 
                                   Category=='C',
                                 as.numeric(NA), value ) )

但是我收到此错误消息:

But I get this error message:

 Error: `true` must be length 1 (length of `condition`), not 1245

我的理解是||是合乎逻辑的&是个人,所以本质上我想说

My understanding is that || is a logical and & is an individual so essentially I want to say

如果(类别B中的NA与AND == 15与)或(类别C中的NA与AND == 15与AND)则使值NA否则使用原始值.

IF (NOT NA AND <=15 AND in category B) OR (NOT NA AND <=15 AND in category C) then make the value NA else use the original value.

我不明白为什么我会误解我|vs ||和&与&&?

I don't understand why I get this error do I misunderstand | vs || and & vs &&?

推荐答案

在这里,问题是使用 || 来返回单个TRUE/FALSE输出而不是 | .根据?"||"

Here, the issue is the use of || which returns a single TRUE/FALSE output instead of |. According to ?"||"

&和&&表示逻辑AND和|和||表示逻辑或.较短的形式以与算术运算符几乎相同的方式执行元素比较.较长的形式从左到右求值,仅检查每个向量的第一个元素.评估仅进行到确定结果为止.较长的形式适合于对控制流进行编程,并且通常是if子句中的首选.

& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.

library(dplyr)
Mydata %>% 
       mutate(value2= if_else(((!is.na(value)) &
                               (value<=0.1) & 
                               (Category=='B')) |
                               ((!is.na(value)) &
                               (OPD_PTNT_PCNT_out<=0.1) & 
                               (Category=='C')),
                               NA_real_, value ) )

这篇关于3条件语句在r中引发长度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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