Control structures in R

R has the appearance of a procedural programming language. However, it is built on another language, known as S programming language. S leans towards functional programming. It also has some object-oriented characteristics. This means that there are many complexities in the way that R works.

In this section, we will look at some of the fundamental building blocks that make up key control structures in R, and then we will move onto looping and vectorized operations.

Assignment operators

R has five assignment operators, which are listed here:

<-

->

=

<<-

->>

In this book, we will use the following assignment operator:

<-

We will use this assignment operator here, because it is used commonly in examples on well-known internet sites such as StackOverflow (http://stackoverflow.com/). It's also possible to use the rightward assignment operator, but that is confusing for many people so it is not used here. Note also that the equals sign isn't used here, because it is often used to mean equality. Therefore, it's clearer to use the leftward assignment operator.

Logical operators

Logical operators are binary operators that allow the comparison of values:

Operator

Description

<

less than

<=

less than or equal to

>

greater than

>=

greater than or equal to

==

exactly equal to

!=

not equal to

!x

Not x

x | y

x OR y

x & y

x AND y

isTRUE(x)

test if X is TRUE

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset