From the course: R for Data Science: Lunch Break Lessons

Vector math

- One of the immediate things you'll bump into with the R programming language is just that it deals with math a little differently than how another standardized programming language might approach it. Let's take a look at that. So for example, if I have a variable or in this case, a vector, how would I multiply each item in that vector by two? First of all, let's create some data we can use here, so I'm going to create something called many.numbers, and into that variable, I'm going to assign the numbers one through nine, so I'll go to one... And hit return, and you'll see that in the global environment to the right I now have a vector called many.numbers that contains the numbers one through nine. Incidentally there's a much quicker way to do this, I can type many.numbers and assign into it 1:9, and that produces exactly the same thing as what I produced before. So now that I have many numbers I would like to multiply each item by two and in the standard language, you would set this up with a for loop. So for something like this it would look for, and then parenthesis a number in many.numbers and then you'll put in curly braces and a space, and we'll just print it out. Print, parenthesis, a number times two. And if I hit return, now that goes through each item in the vector and multiplies it by two. Now, r does this differently, let me show you how r would do it. In this case I would type in many.numbers followed by multiplied by two. And you can see what r has done, has taken each element of many.numbers and multiplied that by two. I can save this result for later by assigning it to a variable, so I'll call something two.times, and put into two.times, many.numbers times two. And you can see over here in the global environment that I have a vector now called two.times with nine numbers and it's been multiplied by two for each of those. So this works for all math operations. So for example I can do many.numbers divided by two and you'll see that the result is each number in many.numbers divided by two. R allows interactions between two vectors. So let's create another vector, we'll call it more.numbers and in it we'll assign... Oh let's assign some random numbers. And you can see that we now have a vector called more.numbers with nine elements and there's a random set of numbers in there. So I can multiply or add those, divide them or subtract them. Let's go ahead and- many.numbers plus more.numbers, and the result will be the first element of many.numbers, added to the first element of more.numbers. So the first element of many.numbers, if I look in the upper right-hand corner in environment, the first element of many.numbers is one, and that's added to the first element of more.numbers which happens to be two, and so the result is three. You can see the result down here is three. So many.numbers plus more.numbers adds each element by element, and of course you can do that with all math operations. One more thing, what happens if the vectors are different lengths? So let's create another vector, I'm going to type down here, short.vector and assign into that, let's just assign three numbers. And now I have a vector called short.vector. What happens if I add many.numbers plus a short.vector? Well, it does something called recycling and what you'll observe here is just that the first number of many.numbers happens to be one. The first number of short.vector happens to be two, and the result is 1 + 2 = 3. Same thing for the second number, 2 + 3 = 5, and 3 + 4 = 7. Now we've just run out of short.vector numbers, so R recycles short.vector, and what we get is four plus the first number of short.vector is two, which is equal to six and so on. So 5 + 3 = 8, and 6 + 4 = 10, that's called recycling. This works because short.vector happens to be a multiple in length of many vectors. If that's not true... Let's create something that will break that, let's call it short.odd.vector, and into it we'll assign four numbers. Now if I attempt to add those two you'll see that we get a warning message, that the longer object length is not a multiple of the shorter object length. So when you're dealing with vectors the idea is that math will work differently as far as adding those two vectors together. You don't need to use a for loop, all of this is built into the way that R thinks.

Contents