R Tutorial : How To Use Conditional Statements in R

preview_player
Показать описание

----

Have a look at the recipe for the if statement: The `if` statement takes a condition; if the condition evaluates to `TRUE`, the R code associated with the if statement is executed. The condition to check appears inside parentheses, while the R code that has to be executed if the condition is `TRUE`, follows in curly brackets.

Let's have a look at an example. Suppose we have a variable `x` equal to -3. If this `x` is smaller than zero, we want R to print out "x is a negative number!". How can we do this using the `if` statement? We first assign the variable, `x` and then write the `if` test.

If we run this bit of code, we indeed see that the string "x is a negative number" gets printed out. However, if we change `x` to 5, and re-run the code, the condition will be FALSE, the code is not executed, and the printout will not occur.

This brings us to the `else` statement: this conditional statement does not need an explicit condition; instead, it has to be used together with an if statement. The code associated with an else statement gets executed whenever the condition of the `if` test is not satisfied. We can extend our recipe by including an else statement as follows:

Returning to our example, suppose we want to print out "x is positive or zero", whenever the condition is not met. We can simply add the else statement:

The else if statement comes in between the if and else statement. To see how R deals with these different conditions and corresponding code blocks, let's first extend our example. We want R to print out "x is zero" if `x` equals 0 and to print out "x is a positive number" otherwise. We add the else if, together with a new print statement, and adapt the message we print on the else statement:
How does R process this control structure? Let's first go through what happens when x equals -3. In this case, the condition for the `if` statement evaluates to `TRUE`, so "x is a negative number" gets printed out, and R ignores the rest of the statements. If x equals 0, R will first check the if condition, sees that it is FALSE, and will then head over to the else if condition. This condition, `x == 0`, evaluates to `TRUE`, so "x is zero" gets printed to the console, and R ignores the else statement entirely. Finally, what happens when x equals 5? Well, the `if` condition evaluates to `FALSE`, so does the `else if` condition, so R executes the else statement, printing "x is a positive number".
Remember that as soon as R stumbles upon a condition that evaluates to `TRUE`, R executes the corresponding code and then ignores the rest of the control structure. This becomes important if the conditions you list are not mutually exclusive.

#DataCamp #RTutorial #IntermediateR
Рекомендации по теме
Комментарии
Автор

greetings from switzerland 🇨🇭
this video was waaaay better understandable than what my teacher tried to teach😂

anthonymanoj
Автор

This was the exact explanation I needed! Can you post the code in the description as well?

scottriley
Автор

Sir please make dinamical program with R progamming

norfatahyo
Автор

Is it possible to use conditions to make 2 correlation tests depending on another factor ? I mean, I have one dichotomic variable and 2 others numeric variables. I want to do a correlation between my 2 numeric variable, but I want to do it according to the dichotomic variable (which corresponds to 2 independent group). Thanks

julietteqb