next up previous
Next: Subsets of data vectors Up: Introduction to S Previous: Data in S

Arithmetic and Logical Operators

S has simple arithmetic operators that work in a fairly intuitive manner. The symbols for addition, subtraction, multiplication, division, and exponentiation are respectively: +, -, *, /, ^. For example:

> x <- 1:5
> x
[1] 1 2 3 4 5
> x+3          # add 3 to each element of x
[1] 4 5 6 7 8
> x*x          # multiply each element of x by itself
[1]  1  4  9 16 25
> x^3          # raise each element of x to the third power
[1]   1   8  27  64 125

S uses the following logical operators:

                >            greater than
                <            less than  
                ==           equal to 
                !=           not equal to
                >=           greater than or equal to
                <=           less than or equal to
                &            and
                |            or
The result of a logical operation is `T' (true) or `F' (false), or a vector of `T's and `F's. As we shall see in the next section, this is very useful for working with subsets of our data. Examples:

> x
[1] 1 2 3 4 5

> x <= 2
[1] T T F F F

> x == 1 | x > 4
[1] T F F F T

next up previous
Next: Subsets of data vectors Up: Introduction to S Previous: Data in S

Albyn Jones
Tue Jun 25 11:03:47 PDT 1996