# Lesson 4 - Vector Math

Vectors of class integer or numeric can participate in mathematical operations. Operations aren't performed on the vector as a whole, though. They are performed element by element. Here is an integer vector with five elements. I'll add three to the vector:

> jersey_numbers <- c(00L, 3L, 32L, 33L, 44L) > jersey_numbers + 3 [1] 3 6 35 36 47

We see that each element of the vector increased by a value of three. Subtraction, multiplication, and division work in the same manner (notice for division, the class was coerced to numeric):

> jersey_numbers - 4 [1] -4 -1 28 29 40 > jersey_numbers * 5 [1] 0 15 160 165 220 > jersey_numbers / 6 [1] 0.000000 0.500000 5.333333 5.500000 7.333333

Let's imagine we are a regional sales manager. The numbers for the first quarter are in. There are 75 new customers for January, 110 for February, and 85 for March. A vector named new_customers is created:

> new_customers <- c(Jan = 75L, Feb = 110L, Mar = 85L) > new_customers Jan Feb Mar 75 110 85

The monthly sales goal is to add 100 new customers. How did we do for Q1? For a vector with such a small number of elements, it's pretty obvious just by looking. But, vector math can also tell us. Here we'll use the > greater than operator:

> new_customers > 100 Jan Feb Mar FALSE TRUE FALSE

From our first quarter data, we also know that we lost 90 customers in January, 80 in February, and 82 in March. A vector named customers_lost is created:

> customers_lost <- c(90L, 80L, 82L) > customers_lost [1] 90 80 82

Now we can determine the number of customers gained vs number of customers lost (plus/minus) for each month of the quarter by subtracting one vector from another. Each vector has the same number of elements (three), and the result is also a vector of three elements:

> net_customer_gain <- new_customers - customers_lost > net_customer_gain Jan Feb Mar -15 30 3

The sum() function can be used to add up all the elements of a vector. Below, we get the total number of new customers and lost customers for the first quarter:

> sum(new_customers) [1] 270 > sum(customers_lost) [1] 252

Did we experience a net gain or loss in customers for the first quarter? Again, the sum() function can tell us:

> sum(net_customer_gain) [1] 18

### Dave's Thoughts

We haven't gotten to any looping or iterative constructs yet in R, but I suspect the sum() function is vastly more efficient than trying to do it yourself. What happens when trying math on vectors with a different number of elements? We get a warning message, along with an answer.

> c(1,2,3) + c(4,4,4,4) [1] 5 6 7 5 Warning message: In c(1, 2, 3) + c(4, 4, 4, 4) : longer object length is not a multiple of shorter object length > c(4,5,6) < c(5,5,5,5) [1] TRUE FALSE FALSE TRUE Warning message: In c(4, 5, 6) < c(5, 5, 5, 5) : longer object length is not a multiple of shorter object lengthWell that's curious. How'd R determine the 4th element in each vector result? I think I have an answer. Let's discuss in the next lesson.

## 0 comments: