loop for in r code example
Example 1: r loops
# Basic syntax of for loop:
for (i in sequence) {
code to be repeated
}
# Example usage of for loop:
for (i in 1:5) {
print(i)
}
# Basic syntax of while loop:
while (condition_is_true) {
code to be repeated
}
# Example usage of while loop:
i = 1
while (i < 5) { # True while i is less than 5
print(i)
i = i + 1 # Increment i each iteration
}
Example 2: r for loop
for (val in sequence)
{
statement
}