【问题标题】:Selecting rows with multiple if and if else statements (R)选择具有多个 if 和 if else 语句的行 (R)
【发布时间】:2020-11-09 11:48:16
【问题描述】:

我尝试用 if 和 else if 语句解决以下问题:

  1. 如果“TRUE1”在“检查”列中明显,则选择带有“TRUE1”的行
  2. 如果“TRUE1”在“检查”列中不明显,则选择带有“TRUE2”的行,否则选择带有“TRUE3”的行

当“检查”列中的“TRUE1”和“TRUE2”可用时,以下代码似乎可以正常工作:

name <- c(1, 2, 3, 4, 5)
check <- c("TRUE1", "TRUE2", "TRUE3", "TRUE3", "TRUE3")
dataset <- data.frame(cbind(name, check))

> dataset
  name check
1    1 TRUE1
2    2 TRUE2
3    3 TRUE3
4    4 TRUE3
5    5 TRUE3

slct_set <- if (dataset$check == "TRUE1") {
  dataset[dataset[, "check"] == "TRUE1",] 
} else if (dataset$check != "TRUE1") {
  dataset[dataset[, "check"] == "TRUE2",]
} else {
  dataset[dataset[, "check"] == "TRUE3",]
}

> slct_set
  name check
1    1 TRUE1

但是,当我对整个“检查”列使用“TRUE3”时,会发生这种情况:

> dataset
  name check
1    1 TRUE3
2    2 TRUE3
3    3 TRUE3
4    4 TRUE3
5    5 TRUE3

> slct_set <- slct_set <- if (dataset$check == "TRUE1") {
  dataset[dataset[, "check"] == "TRUE1",] 
} else if (dataset$check != "TRUE1") {
  dataset[dataset[, "check"] == "TRUE2",]
} else {
  dataset[dataset[, "check"] == "TRUE3",]
}

Warning messages:
1: In if (dataset$check == "TRUE1") dataset[dataset[, "check"] == "TRUE1",  :
  the condition has length > 1 and only the first element will be used
2: In if (dataset$check != "TRUE1") dataset[dataset[, "check"] == "TRUE2",  :
  the condition has length > 1 and only the first element will be used

> slct_set
[1] name  check
<0 Zeilen> (oder row.names mit Länge 0)

我对 R 中的 if 语句很陌生,因此感谢任何帮助。

【问题讨论】:

  • if ... else ... 语句未矢量化。这就是警告消息告诉您的内容。 ifelse() 函数是。这就是你需要的,它会给你正确的答案。

标签: r if-statement rows selection


【解决方案1】:

您可以尝试以下代码,其中 test 是您希望根据它对数据框进行子集化的向量(按降序排列):

name <- c(1, 2, 3, 4, 5)
check <- c("TRUE1", "TRUE2", "TRUE3", "TRUE3", "TRUE3")
dataset <- data.frame(cbind(name, check))

test <- c("TRUE1", "TRUE2", "TRUE3")
dataset[dataset$check == test[min(which(test %in% dataset$check))],]
#>   name check
#> 1    1 TRUE1

对上面代码的一点解释:test %in% dataset$check 检查测试向量的元素是否出现在datasetcheck 列中。 which() 返回结果向量中的位置,其计算结果为 TRUEmin() 因此返回test 的第一个元素,该元素存在于要检查的列中。其余的只是子集。可能比嵌套的 if else 更简单一些。

reprex package (v0.3.0) 于 2020 年 7 月 20 日创建

【讨论】:

    【解决方案2】:

    也许你应该使用%in% 作为if...else... 的条件,如下所示

    if ("TRUE1" %in% dataset$check) {
      dataset[dataset[, "check"] == "TRUE1",] 
    } else if ("TRUE2" %in% dataset$check) {
      dataset[dataset[, "check"] == "TRUE2",]
    } else {
      dataset[dataset[, "check"] == "TRUE3",]
    }
    

    【讨论】:

      【解决方案3】:

      使用 %in% 检查列是否包含文本将是第一步。 (如果满足该条件)您返回相对过滤的数据集

      name <- c(1, 2, 3, 4, 5)
      check <- c("TRUE1", "TRUE2", "TRUE3", "TRUE3", "TRUE3")
      dataset1 <- data.frame(cbind(name, check))
      check <- c("TRUE2", "TRUE2", "TRUE3", "TRUE3", "TRUE3")
      dataset2 <- data.frame(cbind(name, check))
      check <- c("TRUE3", "TRUE3", "TRUE3", "TRUE3", "TRUE3")
      dataset3 <- data.frame(cbind(name, check))
      
      
      func_name = function(dataset){
        if("TRUE1" %in% dataset$check){
          dataset[dataset$check == "TRUE1",]
        }
        else if("TRUE2" %in% dataset$check){
          dataset[dataset$check == "TRUE2",]
        }
        else if("TRUE3" %in% dataset$check){
          dataset[dataset$check == "TRUE3",]
        }
        else{
          "none found"
        }
      }
      
      func_name(dataset = dataset3)
      
        name check
      1    1 TRUE3
      2    2 TRUE3
      3    3 TRUE3
      4    4 TRUE3
      5    5 TRUE3
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-24
        • 1970-01-01
        • 2014-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多