Русские видео

Сейчас в тренде

Иностранные видео




Если кнопки скачивания не загрузились НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если возникают проблемы со скачиванием, пожалуйста напишите в поддержку по адресу внизу страницы.
Спасибо за использование сервиса savevideohd.ru



R Tutorial : How To Use Conditional Statements in R

Want to learn more? Take the full course at https://learn.datacamp.com/courses/in... at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work. ---- 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

Comments