R tutorial - Different Ways to Create and Name Vectors in R

Learn how R applies arithmetic and relational operators to vectors element by element. The tutorial uses a Las Vegas earnings example to show multiplication, subtraction, and the sum() function, demonstrating that R's vector operations differ from traditional matrix math.
Chapters
Before you learned that you can use variables to perform calculus, remember how you summed apples and oranges from the previous video? You also know that actually these variables, my apples and my oranges, are simply vectors. This means that you can perform calculus with vectors in R. You already did it. However, there are still some things I want to discuss about calculus with vectors that contain more than one element. The most important thing to remember about operations with vectors in R is that they will be applied element by element. This means that standard mathematics is extended to vectors in an element wise fashion. To illustrate what this means, let's have a look at an example. Imagine you have a vector containing your gambling earnings for the past three days. Not bad for a few days in the desert, is it? Now imagine a well dressed gentleman approaches you and offers to triple your earnings for the past three days if you beat him in one round of poker. If you want to calculate the expected earnings for each of the past three days, you can easily do this in R. As you can see, R multiplies each element in the earnings factor with 3, resulting in $150 of promised earnings in the first day, 300 in the second day and and 90 in the third day. Sounds like a sweet deal if you ask me. Performing these kind of calculus works just the same with other mathematical operators. Likewise, division, subtraction, summation and many more are all carried out element wise. Just as if you are carrying out the operation between two scalars three times from these lines of code, you don't see anything different from what we've done before. Because of course, you were working with vectors all along. The mathematics naturally extend to vectors that contain more than one element. Let's go back to your Vegas adventures to enjoy your earnings. You also decided to go shopping and spend some money. Every day on the Las Vegas trip, you recorded a vector of expenses. Because you are a very conscious programmer in training, you you decide to compute whether your luck in the casino was sufficient to pay for your expenses. How would you go about this? Once again, this is fairly easy. In R, you can simply subtract expenses from earnings to find your daily balance. This time, the mathematics were also calculated element wise. The first element of expenses was subtracted from the first element of earnings. The second element of expenses was subtracted from the second element of earnings, and so on. It seems that for days one and two you're on the positive side, while on day three you spent more than you earned. Also here, the operations between two vectors that contain more than one element each naturally extend in an element wise fashion. Have a look at these three examples. Pay attention. Multiplication and division in R are different from the traditional matrix and vector calculus, where the multiplications of two vectors can result in a single scalar or a matrix. A last question you might ask yourself is how your bank account progressed overall after these three mad days. In City of Sins, you can save the previous calculation in a new vector called bank and use the sum function on it to calculate the sum of all its elements. Wow, that was pretty close. You still managed to make a $30 profit in Vegas. That's quite an accomplishment. Instead of subtracting expenses from earnings to compare them, which gives you positive and negative numbers, you could also use relational operators to know when your earnings were higher than your expenses. You can use the greater than operator for this. This operator compares the numbers in the vectors. Element wise result is a vector of trues and falses. On days one and two, your earnings exceeded your expenses, so the corresponding elements are true. On the last day, you spent more than you earned, so so the result is false. There are much more relational operators, but that's something for the intermediate R course. For now, you know enough about vector calculus to do some Vegas accounting. Hopefully they can help you to earn some cold hard cash.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







