Video není dostupné.
Omlouváme se.

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

Sdílet
Vložit
  • čas přidán 3. 12. 2015
  • 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: www.datacamp.c...
    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!

Komentáře • 6

  • @wendywei8226
    @wendywei8226 Před 7 lety +4

    Thanks, bro! Good for beginners. Look for more vedios

  • @juanfel1555
    @juanfel1555 Před 7 lety +9

    Do you mean calculations when you say calculus?

  • @General111100
    @General111100 Před 6 lety +1

    in ur website where can i find these videos? i have 1yr subscription yesterday

    • @deepikagautam3071
      @deepikagautam3071 Před 4 lety

      Hi I also want to get subscription of data science can you plz tell about the same for free subscription for a month or 2?

  • @burtthedog4193
    @burtthedog4193 Před 3 lety +2

    This said "calculus" but it has no differentiation or integration. I think you mean "calculations". This is as in other comments below.