简体   繁体   English

R 中带有和/或运算符的 IF/else 语句

[英]IF/else statement with and/or operators in R

I have an idea of what I want to happen but I'm having trouble coming up with the correct If/then statement for it.我对我想要发生的事情有一个想法,但是我无法为其提出正确的 If/then 语句。 There are three specific columns to focus on here: Niu, PVC, and Metal.这里需要关注三个特定的列:Niu、PVC 和 Metal。

If there is a value for Niu but not PVC AND Metal, the row needs to be deleted.如果 Niu 有值但 PVC AND Metal 没有值,则需要删除该行。 It's okay if there is a value for Niu that only has PVC or Metal.如果 Niu 有一个只有 PVC 或 Metal 的值,那也没关系。 If Niu, PVC, and Metal are all Na that row needs to be kept in the data set.如果 Niu、PVC 和 Metal 都是 Na,则该行需要保留在数据集中。

So, the I want rows that have a value for Niu, but not PVC and Metal to be deleted.因此,我希望删除具有 Niu 值的行,但不删除 PVC 和 Metal。

Here is my data:这是我的数据:

structure(list(Seq = c(1L, 2L, 3L, 5L, 6L, 7L), Pag = c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_
), Nucli = structure(c(16L, NA, NA, 16L, NA, NA), .Label = c("2014", 
"2025", "2058", "2073", "2095/1003", "2106", "2140", "2162.7", 
"2195", "2195/3400", "2207.1/3254", "2207.4", "2318", "2329", 
"2407", "2419", "242.1", "2460", "2581", "2581.1", "2581.3", 
"2602", "2624", "2915", "3111", "3122", "3254", "3265", "3298", 
"3298.1", "3298.2", "3400", "3577", "401", "423", "424", "424.2", 
"613", "657", "691", "691.1", "691.2", "768", "78", "813", "813.2", 
"825", "825.2", "836", "836.1", "836.2", "863.1", "869", "869.1", 
"869.2", "981", "981.1", "981.3", "992", "BCN", "Desconegut", 
"P_BCN"), class = "factor"), Niu = structure(c(290L, NA, NA, 
298L, NA, NA), .Label = c("1", "10", "100", "101", "102", "102 verd", 
"103", "104", "105", "106", "107", "108", "109", "11", "110", 
"111", "1118", "112", "113", "114", "115", "116", "117", "118", 
"119", "12", "120", "121", "122", "122 verd", "123", "124", "125", 
"126", "127", "127 verde", "128", "129", "129 verd", "13", "130", 
"131", "132", "132 verd", "133", "134", "135", "136", "137", 
"138", "139", "14", "140", "141", "142", "143", "144", "145", 
"146", "147", "147 verde", "148", "149", "15", "150", "151", 
"152", "153", "154", "155", "156", "157", "158", "159", "159 verd", 
"16", "160", "161", "162", "163", "164", "165", "166", "167", 
"168", "169", "17", "170", "171", "172", "173", "174", "175", 
"176", "177", "178", "179", "18", "180", "181", "182", "183", 
"184", "185", "186", "187", "188", "189", "19", "190", "191", 
"192", "193", "194", "195", "2", "20", "200", "201", "202", "203", 
"204", "205", "205 verd", "206", "207", "208", "208 verd", "209", 
"21", "210", "212", "213", "214", "215", "216", "217", "218", 
"219", "22", "220", "221", "222", "223", "224", "225", "226", 
"227", "228", "229", "23", "230", "230 verde", "231", "232", 
"233", "234", "238", "239", "24", "244", "246", "248", "249", 
"25", "251", "252", "254", "255", "256", "258", "26", "261", 
"263", "264", "266", "267", "27", "271", "273", "276", "279", 
"28", "280", "287", "29", "29 verd", "291", "293", "294", "296", 
"299", "3", "30", "300 gris", "301 gris", "302 gris", "303 gris", 
"304 gris", "305 gris", "306 gris", "307 gris", "308 gris", "309", 
"31", "310 gris", "311 gris", "312", "314", "315 gris", "316 gris", 
"317 gris", "318", "32", "320 gris", "321", "322", "323", "324 gris", 
"325", "327 gris", "33", "331", "34", "343", "345", "35", "350", 
"354", "355", "357", "358", "36", "368", "37", "372", "38", "39", 
"4", "40", "41", "41 verde", "42", "429", "43", "430", "431", 
"433", "435", "436", "438", "439", "44", "440", "442", "444", 
"445", "45", "450", "455", "457", "46", "460", "464", "466", 
"47", "471", "475", "477", "48", "487", "489", "49", "5", "50", 
"500", "51", "52", "53", "54", "55", "56", "57", "58", "59", 
"6", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", 
"7", "7 vermell", "70", "71", "72", "72 verde", "73", "74", "75", 
"75 verd", "76", "77", "78", "781", "79", "8", "80", "81", "82", 
"83", "84", "85", "86", "87", "88", "89", "9", "90", "91", "92", 
"927", "93", "94", "941", "95", "96", "97", "98", "99", "A66", 
"NO_1", "NO_2", "NO_3", "NO_4", "V100", "V102", "V107", "V112", 
"V124", "V126", "V128", "V131", "V132", "V136", "V138", "V139", 
"V62", "V67", "V70", "V79", "V80", "V82", "V87", "V89", "V93", 
"V94", "V95", "V96"), class = "factor"), PVC = c("AMN1", NA, 
NA, "AV52", NA, NA), Metal = c(NA, NA, NA, "6168133", NA, NA), 
    Any.anellament.com.poll = c("2003", NA, NA, "2006", NA, NA
    ), DATA.D.ECLOSIO = c(NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_), DATA.DE.POSTA = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_), ordre_ou = structure(c(8L, 8L, 8L, 8L, 8L, 
    8L), .Label = c("0", "1", "10", "2", "3", "4", "5", "9", 
    "a", "A", "b", "B", "c", "C"), class = "factor"), length = c(6236, 
    6396, 6116, 6162, 5992, 6307), width = c(4463, 4364, 4296, 
    4103, 4337, 4147), Year = structure(c(1L, 1L, 1L, 1L, 1L, 
    1L), .Label = c("2012", "2013", "2014", "2015", "2016", "2017", 
    "2018"), class = "factor")), row.names = c(NA, 6L), class = "data.frame")

Thanks in advance.提前致谢。 Let me know if you have any questions如果您有任何问题,请告诉我

If I understand your requirements correctly, this should do it:如果我正确理解您的要求,应该这样做:

library(dplyr)

df %>% 
  mutate(keep = case_when(!is.na(Niu) & (!is.na(PVC) | !is.na(Metal)) ~ "yes",
                          !is.na(Niu) & (is.na(PVC) & is.na(Metal)) ~ "no",
                          is.na(Niu) & is.na(PVC) & is.na(Metal) ~ "yes")) %>% 
  filter(keep == "yes") %>% 
  select(-keep)

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

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