简体   繁体   English

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

[英]Selecting rows with multiple if and if else statements (R)

I tried to solve the following problem with if and else if statements:我试图用 if 和 else if 语句解决以下问题:

  1. If "TRUE1" is apparent in column "check" select rows with "TRUE1"如果“TRUE1”在“检查”列 select 行中出现“TRUE1”
  2. If "TRUE1 is not apparent in column "check" select rows with "TRUE2" else rows with "TRUE3"如果“TRUE1”在“检查”列中不明显 select 行带有“TRUE2”,否则行带有“TRUE3”

The below code seems to work fine when "TRUE1" and "TRUE2" are available in column "check":当“检查”列中的“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

However, when I use "TRUE3" for the whole column "check" this happens:但是,当我对整个“检查”列使用“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)

I am quite new to if statements in R, so any help is appreciated.我对 R 中的 if 语句很陌生,因此不胜感激。

You could try something like the following code, where test is the vector according to which you would like to subset your dataframe (in descending priority):您可以尝试类似以下代码的代码,其中 test 是您希望根据其对 dataframe 进行子集化的向量(按降序排列):

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

A little explanation to the code above: test %in% dataset$check checks if the elements of the test vector appear in the check column of dataset .对上面代码的一点解释: test %in% dataset$check检查测试向量的元素是否出现在datasetcheck列中。 which() returns the positions in the resulting vector, which evaluate to TRUE . which()返回结果向量中的位置,其计算结果为TRUE min() therefore returns the very first element of test which is existing in the column to check.因此min()返回test的第一个元素,该元素存在于要检查的列中。 The rest is just subsetting. rest 只是子集。 Maybe a bit more straghtforward than a nested if else.也许比嵌套的 if else 更简单一些。

Created on 2020-07-20 by the reprex package (v0.3.0)reprex package (v0.3.0) 于 2020 年 7 月 20 日创建

Maybe you should use %in% for the condition of if...else... like below也许您应该将%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",]
}

Using %in% to check if the column contains the text would be the first step.第一步是使用 %in% 检查列是否包含文本。 The (if that condition is satisfied) you return the relative filtered dataset (如果满足该条件)您返回相对过滤的数据集

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

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

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