选择具有多个 if 和 if else 语句的行 (R)

Selecting rows with multiple if and if else statements (R)

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

  1. 如果“TRUE1”在列“检查”select 行中出现“TRUE1”
  2. 如果“TRUE1”在“检查”列中不明显 select 行带有“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 语句还很陌生,所以非常感谢您的帮助。

您可以尝试类似下面的代码,其中 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() returns 结果向量中的位置,计算结果为 TRUEmin() 因此 returns test 的第一个元素存在于要检查的列中。其余的只是子集化。也许比嵌套的 if else 更直接。

reprex package (v0.3.0)

于 2020 年 7 月 20 日创建

也许你应该使用 %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",]
}

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

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