Common Vector Operations
Now let’s look at some common operations related to vectors. We’ll cover arithmetic and logical operations, vector indexing, and some useful ways to create vectors. Then we’ll look at two extended examples of using these operations.
Vector Arithmetic and Logical Operations
Remember that R is a functional language. Every operator, including
+ in the following example, is actually a function.
> 2+3  5 > "+"(2,3)  5
Recall further that scalars are actually one-element vectors. So, we can add vectors, and the
+ operation will be applied element-wise.
> x <- c(1,2,4) > x + c(5,0,-1)  6 2 3
If you are familiar with linear algebra, you may be surprised at what happens when we multiply two vectors.
> x * c(5,0,-1)  5 0 ...