正确使用“IS NOT”“OR”&嵌套ifelse中的“AND”布尔值

时间:2016-10-12 03:16:07

标签: r if-statement nested boolean

想要根据五种不同的语句使用ifelse创建列。

陈述:

  1. 如果州=“N”“region_1” “00”“99”,请粘贴“region_2”
  2. 如果州=“N”“region_1” 不是“00”“99”,请粘贴“region_1”
  3. 如果state =“T”,请粘贴“region_1”
  4. 如果state =“3”,请粘贴“region”
  5. 如果州=“W”“region_1”“ED”,“FL”,“SG”,“SY”,“XP”,“ AL“,”AG“,”EB“”AB“,粘贴”region_1“
  6. 模拟数据:

    state = c("T","3","W", "W","W","W","W","N","N","N","Q","Q","N","Q")
    region = c("CD","AB","IC","IS", "IE", "IF", "IA", "A2", "A9", "A9", "GW", "AW", "K0", "DW")
    region_1 = c("DG","BC","CL","SY","ED", "FL", "AL", "23", "99", "99", "WB", "WD", "02", 'WW')
    region_2 = c("00", "D1", "05", "00", "00", "01", "59", "00", "23", "24", '03', "03", "37", "03")
    
     a <- data.frame(state, region, region_1, region_2)
    

    我尝试解决:

    library(dplyr)     
    b <- a %>% mutate(t = ifelse(state == "N" & region_1 == "99" | state == "N" & region_1=="00" , region_2,          
                 ifelse(state == "N" & region_1 != "99" | state == "N" & region_1 != "00", region_1,  
                 ifelse(state == "T", region_1,                                                
                 ifelse(state == "3", region, 
                 ifelse(state == "W" & region_1 != "ED" | state == "W" & region_1 != "FL" | state == "W" & region_1 != "SG" | state == "W" & region_1 != "SY" | state == "W" & region_1 != "XP" | state == "W" & region_1 != "AL" | state == "W" & region_1 != "AG" | state == "W" & region_1 != "EB" | state == "W" & region_1!= "AB", region_1,
               NA))))))
    

    问题

    输出存在state == "w"代码问题。它没有认识到我的尝试“不是”并且粘贴"region_1"而不是NA(参见第4:7行)。 state == "N"可能会发生同样的情况,但是,模拟数据中的条件不满足。寻找解决方案。

3 个答案:

答案 0 :(得分:2)

在上一个%in%语句中有一些错误。您可以使用ifelse运算符简化语法。根据您提供的逻辑,我认为您正在寻找的嵌套ifelse(state == "N" & region_1 %in% c("99","00") , region_2, ifelse(state == "N" & ! region_1 %in% c("99","00"), region_1, ifelse(state == "T", region_1, ifelse(state == "3", region, ifelse(state == "W" & ! region_1 %in% c("ED", "FL", "SG", "SY", "XP", "AL", "AG", "EB", "AB"), region_1, NA) )))) 可以表示为:

ifelse

希望有所帮助。

修改

在上一篇 state == "W" & region_1 != "ED" | ... 语句中,考虑逻辑表达式的第一个组成部分:

state = "W"

在数据框的第四行,我们有region_1 = "SY"TRUE & TRUE | ... ,因此上述行将评估为

...

诉讼程序&#39; OR&#39;然后,运算符可以放弃以下语句(...),因为 public class SetDataInArrayList { //Aproach one by using object private List<ActionEvent> list; public SetDataInArrayList() { list = new ArrayList(); } public void setDataInList(ActionEvent e) { list.add(e); } public List<ActionEvent> getList() { return list; } //Approach two by using static reference private static List<ActionEvent> newList; static { newList = new ArrayList<>(); } public static void add(ActionEvent e) { newList.add(e); } public static List<ActionEvent> returnList() { return newList; } 的结果并不重要。

答案 1 :(得分:0)

看起来你可能只是错过了空间中的空间。您目前有region_1!=“AB”尝试添加空格,因此您有region_1!=“AB”

答案 2 :(得分:0)

虽然它的输入量大致相同,但我更喜欢每次更新数据子集时使用data.table的引用更新(:=)。我觉得它更整洁,更容易阅读

library(data.table)

setDT(a)

a[state == "N" & region %in% c("00","99"), t := region_2]
a[state == "N" & !region %in% c("00","99"), t := region_1]
a[state == "T", t := region_1]
a[state == "3", t := region]
a[state == "W" & !region_1 %in% c("ED", "FL","SG","SY","XP","AL","AG","EB","AB"), t := region_1]

a
#     state region region_1 region_2  t
#  1:     T     CD       DG       00 DG
#  2:     3     AB       BC       D1 AB
#  3:     W     IC       CL       05 CL
#  4:     W     IS       SY       00 NA
#  5:     W     IE       ED       00 NA
#  6:     W     IF       FL       01 NA
#  7:     W     IA       AL       59 NA
#  8:     N     A2       23       00 23
#  9:     N     A9       99       23 99
# 10:     N     A9       99       24 99
# 11:     Q     GW       WB       03 NA
# 12:     Q     AW       WD       03 NA
# 13:     N     K0       02       37 02
# 14:     Q     DW       WW       03 NA

# setDF(a)   ## to turn it back into a data.frame