Link Search Menu Expand Document

Introduction

What is a for loop?


For loops allow you to perform an operation iteratively in R (e.g., instead of repeating lines of code). Most functions you use in R are built using for loops.


for (i in x) {

perform some operation using each value of x

}

i stands for iteration, and is set to each value of x in each iteration of the loop

x could be a vector, list, matrix, or any other sequence of values/objects


What is a conditional statement?


Conditional statements allow you to perform operations based on a logical statement, and can be used alone, or often more powerfully within a for loop.


if (logical statement) {

perform some operation if logical statement is true

}

This logical statement can be anything with an outcome of true/false. We will go over examples of these in the tutorial.