Control Flow
Loops are used wherever we have to execute a block of statements repeatedly. For example, printing “hello world” 10 times. The different types of loops in R are –
Example:
R
# R Program to demonstrate the use of # for loop along with concatenate for (i in c (-8, 9, 11, 45)) { print (i) } |
Output:
[1] -8 [1] 9 [1] 11 [1] 45
Example:
R
# R program to demonstrate the # use of while loop val = 1 # using while loop while (val <= 5 ) { # statements print (val) val = val + 1 } |
Output:
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5
Example:
R
# R program to demonstrate the use # of repeat loop val = 1 # using repeat loop repeat { # statements print (val) val = val + 1 # checking stop condition if (val > 5) { # using break statement # to terminate the loop break } } |
Output:
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5
Note: For more information, refer Loops in R
Learn R Programming
R is a Programming Language that is mostly used for machine learning, data analysis, and statistical computing. It is an interpreted language and is platform independent that means it can be used on platforms like Windows, Linux, and macOS.
In this R Language tutorial, we will Learn R Programming Language from scratch to advance and this tutorial is suitable for both beginners and experienced developers).
Contact Us