Basic programming: Introduction
Introduction
R is a full-featured, object-oriented programming language. In general you use programming to get the computer to perform tasks that need to be done many times or to carry out simulations. These tasks can be quite complex, so you often need to control the computational route through conditional statements, conditional repetitions, and by dealing with several cases. Commonly used control structures are:
if
andelse
: a conditional statement in which a condition checked and the program acts on the resultfor
: a finite repetition in which execute a loop of code is execute a fixed number of timeswhile
: a conditional repetition in which execute a loop is executed while a condition is true- repeat: an infinite repetition with explicit
break
to stop it
In this section you will learn more about these control structures and about writing your own functions. Simple examples will illustrate the programming constructs. In addition, the examples will show you how to create nicely formatted messages.
We start this introduction with a short discussion of how to apply R functions repeatedly to components of a data structure.