The Art of R Programming

(WallPaper) #1

7


R PROGRAMMING STRUCTURES


R is a block-structured language in the man-
ner of the ALGOL-descendant family, such
as C, C++, Python, Perl, and so on. As you’ve
seen, blocks are delineated by braces, though
braces are optional if the block consists of just a single
statement. Statements are separated by newline char-
acters or, optionally, by semicolons.
Here, we cover the basic structures of R as a programming language.
We’ll review some more details on loops and the like and then head straight
into the topic of functions, which will occupy most of the chapter.
In particular, issues of variable scope will play a major role. As with many
scripting languages, you do not “declare” variables in R. Programmers who
have a background in, say, the C language, will find similarities in R at first
but then will see that R has a richer scoping structure.

7.1 Control Statements..........................................................


Control statements in R look very similar to those of the ALGOL-descendant
family languages mentioned above. Here, we’ll look at loops andif-else
statements.
Free download pdf