R tutorial - How to Create and Name Vectors in R

Sdílet
Vložit
  • čas přidán 14. 07. 2024
  • In this introduction to R course you will learn how you can create and name your vectors in R.
    Join DataCamp today, and start our interactive intro to R programming tutorial for free: www.datacamp.com/courses/free...
    Hi again! In this video I'll be talking about Vectors. A vector is nothing more than a sequence of data elements of the same basic data type. Remember the atomic vector types I discussed before? You can have character vectors, numeric vectors, logical vectors, and many more.
    First things first: creating a vector in R! You use the `c()` function for this, which allows you to combine values into a vector. Suppose you're playing a basic card game, and record the suit of 5 cards you draw from a deck. A possible outcome and corresponding vector to contain this information could be this one
    Of course we could also assign this character vector to a new variable, drawn_suits for example.
    We now have a character vector, drawn_suits.
    We can assert that it is a vector, by typing is dot vector drawn_suits
    Likewise, you could create a vector of integers for example to store how much cards of each suit remain after you drew the 5 cards. Let's call this vector remain. There are 11 more spades, 12 more hearts, 11 diamonds, and all 13 clubs still remain.
    If you print remain to the console, ..., it looks ok, but it's not very informative. How does somebody else know that the first value corresponds to spades? Wouldn't it be useful if you could attach labels to the vector elements? You can do this in R by naming the vector.
    You can use the `names()` function for this. Let's first create another character vector, `suits`, that contains the strings "spades", "hearts", "diamonds", and "clubs", the names you want to give your vector elements.
    Now, this line of code, ..., sets the names of the elements in `remain` to the strings in suits. If you now print remain to the console, ... , you'll see that the suits information is accompanied by the proper labels. Great! If you don't want to bother with setting the names afterwards, you could just as well create a named vector with a one liner. You can use equals signs inside the `c()` function:
    Notice that here, it's not necessary to surround the names, "spades", "hearts", "diamonds" and "clubs", with double quotes, although this also works perfectly fine.
    In all three cases, the result is exactly the same. Under the hood, R vectors have attributes associated with them. What you did when you set the names of the remain vector, is actually setting the names attributes of the remains object. The `str()` function, that compactly displays the the structure of an R object, shows this.
    You'll have plenty of fun creating and naming variables in all sorts of ways, but before I let you to it, there are two more things I want to discuss with you.
    First of all, remember the variables you've created in the previous chapter? These variables, such as `my_apples`, equal to 5, and `my_oranges`, equal to the character string "six" at some point, are actually all vectors themselves. R does not provide a data structure to hold a single number or a single character string or any other basic data type: they're all just vectors of length 1. You can check this by typing is dot vector my_apples, which is TRUE, and is dot vector my_oranges, which is TRUE as well.
    That these variables are actually vectors of length 1, can be checked using the length() function.
    This contrasts with the other vectors we've created in this video: the drawn_suits vector, for example, has length 5.
    The last important thing is that in R, a vector can only hold elements of the same type. They're also often called *atomic vectors*, to differentiate them from *lists*, another data structure which can hold elements of different types. This means that you cannot have a vector that contains both logicals and numerics, for example. If you do try to build such a vector, R automatically performs coercion to make sure that you end up with a vector that contains elements of the same type. Let's see how that works with an example.
    In contrast to recording the suits you draw from a deck of cards, suppose now you're recording the ranks of the cards. You might want to combine the result of drawing 8 cards like this, creating a vector drawn_ranks.
    If you now inspect this vector, you'll see that the numeric vector elements have been coerced to characters, to end up with a homogeneous character vector.
    This is also what the class function from before tells us.
    The fact that R handles this for us automatically, 'upgrading' logicals to numerics and numerics to characters when necessary along the way, is useful but can also be dangerous, so be aware of this. If you want to store elements of different types in the same data structure, you'll want to use a list. But that's something for later.
    Now, it's time to step up your betting game in the interactive exercises!

Komentáře • 21

  • @cassm4687
    @cassm4687 Před 5 měsíci +5

    Thank you😭 TA very unhelpful, I’ve been sitting here trying not to cry after trying and failing for hours but your video helped me in less than 5 minutes💜💜
    - thank you, very grateful!

  • @mentefria98
    @mentefria98 Před 2 lety +6

    Men, I loved the way you explained everything, very calmed and making everything look simple. Thanks for this awesome explanation

  • @ChristyDuvalRN
    @ChristyDuvalRN Před 4 lety +1

    I'm taking this on the website and app. I was getting a little lost but these videos have been very helpful. Thank you.

  • @zhaofour9833
    @zhaofour9833 Před 7 lety +8

    This video answers my biggest question!!! Thanks man, you are awesome

    • @DataCamp
      @DataCamp  Před 7 lety +2

      Hi Frank, that's great to hear!

  • @semperdiscendum7439
    @semperdiscendum7439 Před 6 lety

    Loved it!! Thank you!!👍👍

  • @snehithreddygankidi5533
    @snehithreddygankidi5533 Před 7 lety +8

    excellent work and the teaching is so awesome, anyone can understand and It covers from low level to high level .looking forward for more videos on other languages

  • @sundusy4345
    @sundusy4345 Před 2 lety

    Thank you :) please continue making videos it was truly helpful.

  • @yanweili6951
    @yanweili6951 Před 4 lety

    thx thats clear n helpful

  • @sumitpoojary7540
    @sumitpoojary7540 Před 7 lety +11

    There is small mistake at 1:52 you said to punch in suits but the demo prints remain. It has caused me a little confusion.

    • @DataCamp
      @DataCamp  Před 7 lety +3

      Hi Sumit! Thanks for submitting this feedback to us! Sorry for the confusion!

  • @m.d.2788
    @m.d.2788 Před 6 lety

    at 2:40 what does the -attr(*,"names") doing? I am googling without any luck. thanks in advance!

  • @Itthew
    @Itthew Před 5 lety

    Why are there double quotation mark for each character element but not for numeric? I am kinda confused. Thanks !

  • @marwannaciri1926
    @marwannaciri1926 Před 5 lety +1

    Is there a way to create a vector containing the values of a given column of a datafile ?

  • @muhammadabduh8159
    @muhammadabduh8159 Před 4 lety

    3:24

  • @akashdeepchoudhury
    @akashdeepchoudhury Před 7 lety

    where can i find the exercise?

    • @DataCamp
      @DataCamp  Před 7 lety

      Chapter 2 of our intro to R course: www.datacamp.com/courses/free-introduction-to-r

  • @somethingrandomidontknow7945

    though this was a lesson for math G12 dint expect it to be about Coding Bruh

  • @fouziasubhani9138
    @fouziasubhani9138 Před 2 lety

    I DO NOT HAVE ANY KNOWLAGDE ABOUT PROGRAMMING. BUT FOR MY MASTERS STUDIES R PROGRAMMING IS MANDATORY. ANYONE CAN GUIDE ME FROM WHERE I HAVE TO START LEARNING?

  • @evanturzanski9265
    @evanturzanski9265 Před 4 lety

    Like #400