October 2011
Beginner to intermediate
400 pages
9h 30m
English
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.
Remember that R is a functional language. Every operator, including + in the following example, is actually a function.
> 2+3 [1] 5 > "+"(2,3) [1] 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) [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) [1] 5 0 ...
Read now
Unlock full access