English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Conditional Statements

Die Entscheidungsstruktur erfordert, dass der Programmierer eine oder mehrere zu bewertende oder zu testende Bedingungen und die Anweisungen spezifiziert, die ausgeführt werden sollen, wenn die Bedingungen wahr sind (notwendig) und die Anweisungen, die ausgeführt werden sollen, wenn die Bedingungen falsch sind (optional).

Dies ist die allgemeine Form der typischen Entscheidungsstruktur in den meisten Programmiersprachen:

R bietet die folgenden Arten von Entscheidungsanweisungen an:

  • if-Anweisung

  • if...else-Anweisung

  • switch statement

if-Anweisung

eine "if"-Anweisung besteht aus einem Boolean-Ausdruck gefolgt von einem oder mehreren Anweisungen.

The syntax format is as follows:

if(boolean_expression) {
    // wird ausgeführt, wenn der Boolean-Ausdruck wahr ist
}

wird ausgeführt, wenn der Boolean-Ausdruck "boolean_expression" wahr ist, wenn falsch, wird nicht ausgeführt.

x <- 50L
if(is.integer(x)) {
   print("X" ist eine ganze Zahl)
}

Execute the above code, and the output result is:

[1] "X" ist eine ganze Zahl

if...else-Anweisung

eine "if"-Anweisung kann eine optionale "else"-Anweisung haben, die ausgeführt wird, wenn der Boolean-Ausdruck falsch ist.

The syntax format is as follows:

if(boolean_expression) {
    // wird ausgeführt, wenn der Boolean-Ausdruck wahr ist
} else {
    // wird ausgeführt, wenn der Boolean-Ausdruck falsch ist
}

wenn der Boolean-Ausdruck "boolean_expression" wahr ist, wird der Code im "if"-Block ausgeführt. Wenn der Boolean-Ausdruck falsch ist, wird der Code im "else"-Block ausgeführt.

x <- c("google","w3codebox,"taobao")
if("w3"codebox" %in% x) {
   print("enthält "w3codebox)
} else {
   print("enthält nicht "w3codebox)
}

Execute the above code, and the output result is:

[1] "enthält "w3codebox"

wenn mehrere Bedingungen überprüft werden müssen, kann "if...else if...else" verwendet werden:

else if(boolean_expression 1) {
    // wenn der Boolean-Ausdruck "boolean_expression" 1 wird ausgeführt, wenn der Boolean-Ausdruck wahr ist
} 2) {
    // wenn der Boolean-Ausdruck "boolean_expression" 2 wird ausgeführt, wenn der Boolean-Ausdruck wahr ist
} 3) {
    // wenn der Boolean-Ausdruck "boolean_expression" 3 wird ausgeführt, wenn der Boolean-Ausdruck wahr ist
} else {
    // alle Boolean-Ausdrücke sind falsch, dann wird der folgende Code ausgeführt
}
x <- c("google","w3codebox,"taobao")
print("erster "if" enthält "weibo")
   else if("w
}3"codebox" %in% x) {
   print("zweiter "if" enthält "w3codebox)
} else {
   print("Not found")
}

Execute the above code, and the output result is:

[1] "The second if contains w3codebox"

switch statement

A switch statement allows testing the situation when a variable equals multiple values. Each value is called a case.

The syntax format is as follows:

switch(expression, case1, case2, case3....)

switch statements must follow the following rules:

  • switch In the statement expression It is a constant expression, which can be an integer or a string. If it is an integer, then it returns the corresponding case position value, and if the integer is not within the range of positions, then it returns NULL.

  • If multiple values match, then return the first one.

  • expressionIf it is a string, then it corresponds to the value of the variable name in the case, and if there is no match, then there is no return value.

  • switch has no default parameter available.

The following example returns the third value:

x <- switch(
   3,
   "google",
   "w3codebox",
   "taobao",
   "weibo"
)
print(x)

Execute the above code, and the output result is:

[1] "taobao"

If it is a string, then return the value corresponding to the string variable:

you.like<-"w3codebox"
switch(you.like, google="www.google.com", w3codebox = "de.oldtoolbag.com", taobao = "www.taobao.com")

Execute the above code, and the output result is:

[1] "de.oldtoolbag.com"

If the integer is not within the range, then return NULL

> x <- switch(4,"google","w3codebox,"taobao")
> x
NULL
> x <- switch(4,"google","w3codebox,"taobao")
> x
NULL