📜  R 中的嵌套 if-else 语句

📅  最后修改于: 2022-05-13 01:55:04.285000             🧑  作者: Mango

R 中的嵌套 if-else 语句

在本文中,我们将讨论 R 编程语言中的嵌套 if-else 语句。

if-else 语句可以嵌套在一起形成一组语句,并根据条件逐一计算表达式,分别从外层条件开始到内层条件。另一个 if-else 语句中的 if-else 语句更好地证明了定义的合理性。

句法:

if(condition1){
# execute only if condition 1 satisfies
if(condition 2){ 
# execute if both condition 1 and 2 satisfy
}
}else{
}

示例:嵌套的 if-else 语句

R
# creating values
var1 <- 6
var2 <- 5
var3 <- -4
  
# checking if-else if ladder
if(var1 > 10 || var2 < 5){
  print("condition1")
}else{
  if(var1 <4 ){
    print("condition2")
  }else{
    if(var2>10){
      print("condition3")
    }
    else{
      print("condition4")
    }
  }
}


R
# creating a dataframe 
data_frame <- data.frame(col1 = c(1:9),
                         col2 = LETTERS[1:3])
  
print("Original DataFrame")
print(data_frame)
  
data_frame$col3 = ifelse(data_frame$col1>4,"cond1 satisfied",
                         ifelse(data_frame$col2 %in% c("A","C"),
                                "cond2 satisfied",
                         "both failed"))
  
print("Modified DataFrame")
print(data_frame)


R
# creating a dataframe 
data_frame <- data.frame(col1 = c(1:9),
                         col2 = LETTERS[1:3])
  
print("Original DataFrame")
print(data_frame)
  
data_frame$col3 = with(data_frame, 
                       ifelse(col1>4,"cond1 satisfied",
                        ifelse(col2 %in% c("A","C"),
                               "cond2 satisifed",
                           "both failed")))
    
print("Modified DataFrame")
print(data_frame)


输出:

[1] "condition4"

使用 ifelse 语句

ifelse() 方法中的第一个参数包含要评估的条件。第二个和第三个参数分别包含条件的真假评估值。在使用数据框或其他 R 对象进行评估的情况下,列通常使用数据框名称进行引用。

句法:

ifelse(cond, value-on-true, value-on-false)

示例:使用 ifelse 嵌套 if-else

电阻

# creating a dataframe 
data_frame <- data.frame(col1 = c(1:9),
                         col2 = LETTERS[1:3])
  
print("Original DataFrame")
print(data_frame)
  
data_frame$col3 = ifelse(data_frame$col1>4,"cond1 satisfied",
                         ifelse(data_frame$col2 %in% c("A","C"),
                                "cond2 satisfied",
                         "both failed"))
  
print("Modified DataFrame")
print(data_frame)

输出:

[1] "Original DataFrame" 
col1 col2 
1    1    A 
2    2    B 
3    3    C 
4    4    A 
5    5    B 
6    6    C 
7    7    A 
8    8    B 
9    9    C 
[1] "Modified DataFrame"
col1 col2            col3 
1    1    A cond2 satisfied 
2    2    B     both failed 
3    3    C cond2 satisfied 
4    4    A cond2 satisfied 
5    5    B cond1 satisfied 
6    6    C cond1 satisfied 
7    7    A cond1 satisfied 
8    8    B cond1 satisfied 
9    9    C cond1 satisfied

在嵌套数据框的情况下,嵌套条件一次又一次地包含数据框名称。为了避免这种复杂性并提高效率,数据帧名称在 with() 方法的第一个参数中指定。

句法:

with(data-frame , ifelse())

示例:将 with() 与 ifelse() 一起使用

电阻

# creating a dataframe 
data_frame <- data.frame(col1 = c(1:9),
                         col2 = LETTERS[1:3])
  
print("Original DataFrame")
print(data_frame)
  
data_frame$col3 = with(data_frame, 
                       ifelse(col1>4,"cond1 satisfied",
                        ifelse(col2 %in% c("A","C"),
                               "cond2 satisifed",
                           "both failed")))
    
print("Modified DataFrame")
print(data_frame)

输出

[1] "Original DataFrame" 
col1 col2 
1    1    A 
2    2    B 
3    3    C 
4    4    A 
5    5    B 
6    6    C 
7    7    A 
8    8    B 
9    9    C 
[1] "Modified DataFrame"
col1 col2            col3 
1    1    A cond2 satisfied 
2    2    B     both failed 
3    3    C cond2 satisfied 
4    4    A cond2 satisfied 
5    5    B cond1 satisfied 
6    6    C cond1 satisfied 
7    7    A cond1 satisfied 
8    8    B cond1 satisfied 
9    9    C cond1 satisfied