Introduction to R for Data Science (Part Four)
This is the fourth introduction to R. This will cover functions, while loops, for loops, built-in features, and more.
Originally published on my Substack. This is just a part of the article.
PS: Please read ‘Introduction to R for Data Science (Part Three)’ before reading this one. This is a continued version of part three.
Part three: Introduction to R for Data Science (Part Three)
If, else, and else if Statements
The logic behind the if, else, and else if statement is the same among other programming languages. The syntax is different.
NOTE: You can’t use the console when writing if, else, and else if statements. You have to use the R script.
15 equals the if statements, so it printed out ‘x is equal to 15!’
If it was 11 it would print out the else-if statement, ‘x is equal to 11’
13 doesn’t equal 15 or 11, so it displays the else condition.
You want to write out the if statement first, then else if statement, and last the else statement. If 13 doesn’t equal 15, it will go to the else if statement. If 13 doesn’t equal 11, it will go to the else statement. This would be your last statement and you would want to write a condition when everything fails. That is why you don’t want to write else (x == 10). It wouldn’t work anyways.
If you want more conditions, you can add more else-if statements.
Here is an example that you can write on your R script. Play around with it and see if you can understand it.
While Loops
While loops will let our program run basically forever until a condition is met.
In this example, x would continue on running until conditions are met. X has run until four since four is the last digit less than five.
You can add an if statement on a while loop.
Use a break statement if you just want to break out of the loop. This would stop the looping.
For Loops
For loops will
Read the full article here: https://ivanh.substack.com/p/introduction-to-r-for-machine-learning-d0a