In this introduction to R course you will learn about the different ways to create and name your vectors in R.
Join DataCamp today, and start our interactive intro to R programming tutorial for free: https://www.datacamp.com/courses/free-introduction-to-r
Before, you learned that you can use variables to perform arithmetic 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 arithmetic with vectors in R; you already did it! However, there are still some things I want to discuss about calculations with vectors that contain more than 1 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 3 days.
Not bad for a few days in the desert, is it? 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 it in R.
As you can see, R multiplies each element in the `earnings` vector with 3, resulting in 150 dollars of promised earnings in the first day, 300 in the second day and 90 in the third day. Sounds like a sweet deal if you ask me!
Performing these kinds of calculations works just the same with other mathetemical 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 Strip. You recorded a vector of expenses.
Because you are a very conscious programmer in training, 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 1 and 2, you are on the positive side, while on day 3 you spent more than you earned.
Also here, the operations between two vectors that contain more than 1 element each, naturally extend in an element-wise fashion. Have a look at these three examples.
Pay attention here: multiplication and division in R are different from the traditional matrix and vector operations, where the multiplication 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 the 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 thirty dollar 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 earnings were higher than expenses. You can use the greater than operator for this. This operator compares the numbers in the vectors element-wise:
The result is a vector of TRUE's and FALSE's: on days 1 and 2, your earnings exceeded your expenses, so the corresponding elements are `TRUE`. On the last day you spent more than you earned 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 arithmetic to do some Vegas accounting; hopefully they can help you to earn some cash!