To create a for
loop in R, you need to specify the start point, the end point and an increment (optional) value. Once the loop is executed, each value between the start point and end point will be processed one at a time.
Here's an example of a for
loop that iterates through a sequence of numbers and prints each value:
main.r30 chars4 lines
Output:
main.r30 chars6 lines
In the above example, i
starts from 1 and increments by 1 up to 5. The loop body executes once for each value of i
.
You can iterate through other objects as well such as a vector or a list. Here's an example of a for
loop that iterates through a list
of names and prints each name:
main.r89 chars6 lines
Output:
main.r34 chars4 lines
In the above example, name
takes values from the names_list
list one at a time, and the loop body prints each value.
gistlibby LogSnag